Re: C code... file reading(twice)... strange problem!
Abhijit Menon-Sen <[email protected]> Wed, 23 Mar 2005 19:49:56 +0530
| Newsgroups | gmane.user-groups.linux.delhi.devel |
|---|---|
| Message-ID | <[email protected]> |
At 2005-03-23 06:12:17 -0800, rohitsz2-/[email protected] wrote: > > > > ... > > > fread(&val,sizeof(long),1,fs); > > ^^^^ > > Similarly, you want just "val" here. (I note, however, that you're > > overwriting your loop counter; and it works only because the value > > you read from the file is the value of the loop counter anyway.) > > done that...( i am not overwriting my loop counter(ie > i=val, i am not doing any such thing... so where am i > overwriting?!, kindly point out!) > though i must tell when i wrote into the file with When you call fread above, it reads 1*sizeof(long) bytes from fs, and writes it to *val. Since val == &i, it overwrites the value of i. The only reason it works is because the value of i just happens to be the same as the value read from the file (because that's how you wrote it in the first loop). > > > for(i=0;i<n;i++) > > > {/* fread reads same value every time!*/ > > > fread(&val,sizeof(long),1,fp); > > ^^^^ > > First, you want just "val" here. Second, val is uninitialised here, > > so this code just won't work. Try making val actually point to > > something. > > done this too...(i can't understand, why to make val > point to something..? i am reading anything before > writing something in it!) Huh? val must point to valid storage for 1*sizeof(long) bytes for fread to work. -- ams