Re: C code... file reading(twice)... strange problem!

Rohit Kumar <rohitsz2-/[email protected]> Wed, 23 Mar 2005 06:12:17 -0800 (PST)
Newsgroups gmane.user-groups.linux.delhi.devel
Message-ID <[email protected]>
--- Abhijit Menon-Sen <[email protected]> wrote:
> 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.

Done that...

> > ...
> > 		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    
                 
                  temp=rand()
                  val=&temp;
                  fwite(&val,sizeof(long),1,fs);
then the code writes junk values!

> > ...
> > 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.

done this too...(i can't understand, why to make val
point to something..? i am reading anything before
writing something in it!)

i wrote in one of previous reply's... when i replace
&val with val, it works fine with TC but not with gcc,
it gives a segmentation fault with gcc... so i am
really surprised to see the varying outputs!

thanks & regards,
-rohit.

NAMASTE, i honour the spirit in you which is also in me.
```````````````````````````````````````````````````````
http://www.geocities.com/rohitsz2

|Anytime you feel i need to understand| 
|a concept before i am eligible to get|
|an answer i would be happy to know.  |
^*************************************^

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com