Hi all
I don't know if I can ask questions related to C-without-plus here, but can you please help me, this is an important exercise I have to finish.
I was coding with C in ubuntu when I compiled one file with these lines:
Input file is a text file formatted like this
1 2 3
|
1 20091480 Jones Smith 3.5
2 20101598 Vladimir Ilich Lenin 6.8
3 20112266 Just Another Name 5.2
| |
and so on
After that, these codes are used to read from such a text file and then write into a binary file
1 2 3 4 5
|
typedef struct {
char name[30];
float sc;
int id;
} Student;
| |
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for(i=0;!feof(fpI);i++){
st=stud+i*sizeof(Student);
fscanf(fpI,"%d",&x);
fscanf(fpI,"%d",&(*st).id);
if((*st).id==0) break;
fgets(buf,34,fpI);
for(dot=strlen(buf);buf[dot]!='.';dot--);
(*st).sc=(buf[dot+1]-'0')*.1+buf[dot-1]-'0';
strncpy((*st).name,buf,strlen(buf)-5);
(*st).name[strlen(buf)-5]='\0';
fwrite(st,sizeof(Student),1,fpO);
printf("\n%2d %9d %s %4.1f",i+1,(*st).id,(*st).name,(*st).sc);
}
| |
After compiling, the output execut didn't work and showed that it couldn't make a proper output. I checked with cat command and found that: the first two records available in the input text file were missing in the output binary file. The output file started with
\2252 Just Another Name ^@^@^@^@^@^@^@....
You can notice that I put a printf line; it was for debugging use. It brought to the screen exact results, I mean nearly exactly the same as the input text.
This is not the first time I met such a "two first line gone missing" issue, I can't understand why, can you please help me out?
If you want to know more about the environment, I used Emacs(GTK) and Ubuntu 9.10 and GCC.
Can you please tell me what kind of problem that is, why it occured and how to solve it?