fwrite() results in data lost in C-without-plus

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
 
int x,dot; char buf[30];

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?
C questions are welome.

I don't quite understand what this is doing:
st=stud+i*sizeof(Student);

I would expect the loop to go something like:
1
2
3
4
5
6
for (i = 0; !feof(fpI); ++i)
{
    Student st;
    /* read line into st */
    fwrite(st,sizeof(Student),1,fpO);    /* write st to file: you already have this */
}


I've not done the parsing, I assume you can do that. At any rate, don't do it in the loop directly, move that code out into a sepeate function. If you're stuck with that bit, we can help out.
Last edited on
My guess is that st=stud+i*sizeof(Student) line is trying to point to the next structure in an array of Student structures. If so, you could use array syntax. How are st and stud defined?

One potential problem I see with your fgets() call is that you're trying to read up to 34 bytes into an array of 30 characters. This will work as long as there are fewer than twenty-nine characters at or after the current file pointer (or there is a newline character or you reach the end of your input file.) I would make your input buffer much larger for saftey's sake.

Also, you can reach the end of your input file after one of your fscanf() calls: Any other file I/O after that may cause serious problems. So, check your returns from your fscanf() calls.
Yeah, sorry I made some mistakes while describing the problem. st and stud are pointers pointing to records of Student, Student *st,*stud;where stud is the ptr to mark where-in-mem the previously-allocated memory started, and st is used to stop at every record to get that record processed. And I've replaced 30 with 50, I'll try to be less careless next time.
I'm trying to get used to record-ptrs and binary files... I hate problems that should never occur in theory but somehow happens in reality
Topic archived. No new replies allowed.