Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to compile this program under Windows
(it's a program which turns bootable code in floppies. I got the source from here: http://www.acm.uiuc.edu/sigops/roll_your_own/1.bootstrap.html).
First I got the problem that it didn't read the INI file. That is solved now. Now I get a segfault on the following rule:
while(data < end)
I added output to the app, so it should be clear where the executing stops: no "."'s are printed out. I'm on Win64, with the Cygwin toolchain.
Thanks!
Yvan
A mistake is:
size = (int *)lSize;
Better:
*size = lSize;
and the function as:
void *loadfile(char *file, long *size)
...
and in calling context eg:
long size=0;
char *buffer = loadfile("blah.txt",&size);
if( buffer )
{
printf("\nstrlen = %lu, fsize = &ld", strlen(buffer), size );
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am doing a project in my class about file and i/o system calls. I have dumbed down the code below for the sake of my question. I have read the manual multiple times. I thought I understood read(), but clearly I'm missing something.
int a, bytesRead;
char buffer[150];
a = open("/home/JohnDoe/Dowloads/TestFile.dat", 0);
if (a < 0) {
printf("Error opening file TestFile.dat\n");
return 0;
}
bytesRead = read(a, buffer, 150);
printf("Bytes read: %d\n", bytesRead);
I'm lost to as why this read() call returns -1. If I understand the manual correctly, read() takes arguments file id, buffer and number of bytes to read. The integer, a (file id), was returned without error, I created my buffer and reading 150 bytes should be no problem because the file contains well over 150 bytes. Any help would be greatly appreciated.
Check errno. If read returns -1, the errno will be set and you can see, where the error happened.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I keep running a memory leak when I print a bunch of times to a file by calling this function multiple times. There aren't any issues unless I actually print.
void print_fields_weights_energies(MKL_Complex16 *stored_fields,
MKL_Complex16 *stored_energies,
MKL_Complex16 *weights,
int steps,
int_st ist,
cns_st cns) {
/*Print The Fields Out To File*/
int walkers;
int fields;
int field_number;
double tau = steps*cns.dtau;
char message[150];
FILE *pf;
/*Name and Make File*/
sprintf(message, "fields-tau%f.dat", tau);
pf = fopen(message, "w+");
for (walkers=0; walkers<cns.max_number_walkers; walkers++) {
for (fields=0; fields<ist.n_spin_orbitals_sq; fields++) {
field_number = walkers*ist.n_spin_orbitals_sq*3+fields*3;
fprintf(pf, "%f\t %d\t %d\t", tau, walkers, fields);
fprintf(pf, "%f\t %f\t %f\t %f+%fi\t\t", stored_energies[walkers*3].real, stored_energies[walkers*3+1].real, stored_energies[walkers*3+2].real, weights[walkers].real, weights[walkers].imag);
fprintf(pf, "%f\t %f+%fi\t %f+%fi\n", stored_fields[field_number].real, stored_fields[field_number+1].real, stored_fields[field_number+1].imag, stored_fields[field_number+2].real, stored_fields[field_number+2].imag);
}
}
fflush(pf);
return;
}
You need an fclose() at the bottom of your function (and you don't need the fflush()--the file will get flushed when it gets closed).
You also should be checking for (pf == NULL) before referencing it, in case of an error on the fopen(). Getting an error message is far more useful than getting a SEGV.
For future memory leaks, I highly recommend the tool "valgrind"--it tends to point you to exactly where your memory leak occurs.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.
You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am coding C language in Visual Studio C++.
In the first calling of this function there is no problem. It makes all of process but in the second calling VS give "Windows has triggered a breakpoint" error.
However, I compile and run the code in Linux, there is no problem.
void printDataPagePersons(int pageNumber)
{
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
printf("page of city : %d\n",sizeof(*pageofCity));
FILE* fp;
fp=fopen("x.dat", "rb");
fseek(fp, PAGE_SIZE*pageNumber,SEEK_SET);
fread(pageofCity, PAGE_SIZE,1, fp);
fclose(fp);
//OTHER PRINTING PROCESSES...
}
Here you allocate a buffer:
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
This is the size of that buffer:
sizeof(Person)* RECORD_COUNT
You then read data from a file into that buffer at this line of code:
fread(pageofCity, PAGE_SIZE,1, fp);
There you are telling the fread function to read a PAGE_SIZE of bytes into that buffer.
I'm guessing the cause of your problems is this condition is also true:
PAGE_SIZE > sizeof(Person)* RECORD_COUNT
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to read from a fifo and i am not understanding it's behaviour.
This is the write side, write.c:
for(int i = 1;i<argc;i++){
if(write(fifoFd,argv[i],strlen(argv[i])) <= 0)
perror("Error writing");
}
And this is the read side, read.c:
char buf[1024];
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
First i start read.c ./read then i execute ./write
If i execute write like this ./write backup *.txt sometimes i get what i expected, i.e, backupexample1.txtexample2.txt.
But sometimes i only get example1.txtexample2.txt and i am not understanding this, where is "backup"?
Your code:
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
You loop, each time through the loop you overwrite the buffer, and then you print the buffer. So, sometimes, you read "backup" followed by "example1.txtexample2.txt" (which overwrites "backup"), other times you read the whole lot at once in a single read.
If you change the loop to read into the unpopulated portion of the buffer, it will behave consistently:
int read = 0;
while(read != 1024){
int b = read(fifoFd,buf+read,1024-read);
if(b<=0) break;
read += b;
}
printf("%s\n",buf);