Re: C code... file reading(twice)... strange problem!
Abhijit Menon-Sen <[email protected]> Tue, 22 Mar 2005 00:50:51 +0530
| Newsgroups | gmane.user-groups.linux.delhi.devel |
|---|---|
| Message-ID | <[email protected]> |
At 2005-03-19 05:10:42 -0800, rohitsz2-/[email protected] wrote: > > long i,*val; > ... > for(i=0;i<LT;i++) > { > val=&i; > fwrite(&val,sizeof(long),1,fs); ^^^^ You should pass just "val" to fwrite here. > ... > 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.) > ... > long* getRandomNumArray(long n) > { > long *base,*ptr,*val; > ... > 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. -- ams