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

Brijesh Melikadan <[email protected]> Mon, 21 Mar 2005 19:42:32 +0530
Newsgroups gmane.user-groups.linux.delhi.devel
Message-ID <[email protected]>
On Sat, 19 Mar 2005 05:10:42 -0800 (PST), Rohit Kumar
<rohitsz2-/[email protected]> wrote:
> 
> Sorry, posted a 'code under debugging' file, apologies
> for that...
> the original code file is here...
> 
> --- Rohit Kumar <rohitsz2-/[email protected]> wrote:
> > Greetings there,
> > i am getting strange result in the following code...
> > what i am doing in the code...
> >
> > 0.main
> > 1.create a file
> > 2.write numbers into it
> > 3.close file
> >
> > 4.open file in read mode
> > 5.read numbers written
> > 6.close file
> >
> > 7.call a function
> > 8.open file(same file)
> > 9.read numbers
> > 10.assign it to a pointer
> > 11.close file
> > 12 return pointer
> >
> > 13.print numbers from the pointer received in main
> > 14.end-main
> >
> >
> > upto step 8 things are ok..
> > after that when i try to read the numbers from the
> > file, it always returns the last number that was
> > written to the file...
> > here goes the code...
> >
> -------------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> 
> #define LT 10   /* 10 */
> 
> long* getRandomNumArray(long);
> 
> int main()
> {
> 
> long i,*val;
> FILE *fs;
> long *li,*lb;
> 
> fs=fopen("temp.dat","w");
> 
> if(fs==NULL)
>        {
>                printf("Error opening file[w]\n");
>        }
> else
> {
> 
>        for(i=0;i<LT;i++)
>        {
>                val=&i;
>                fwrite(&val,sizeof(long),1,fs);
>                printf("%ld\t",*val);
>        }
> 
> }
> if(fs!=NULL)
>        fclose(fs);
> 
> printf("\n");
> 
> fs=fopen("temp.dat","r");
> if(fs==NULL)
>        {
>                printf("Error opening file[r]\n");
>        }
> else
> {
>        for(i=0;i<LT;i++)
>        {
>                fread(&val,sizeof(long),1,fs);
>                printf("%ld\t",*val);
>        }
> 
> }
> if(fs!=NULL)
>        fclose(fs);
> 
> printf("\n");
> 
>        lb=getRandomNumArray(LT);
>        li=lb;
>        if(li!=NULL)
>        {
>        printf("printing number\n");
>        for(i=0;i<LT;i++)
>                printf("%ld \t",*li++);
>        }
> printf("\n");
> return 0;
> }/*main*/
> 
> long* getRandomNumArray(long n)
> {
>        long *base,*ptr,*val;
>        long i; int p;
>        FILE *fp;
> 
>        fp=fopen("temp.dat","r");
> 
>        if(fp!=NULL)
>        {
> 
>                ptr=(long *)malloc(n*sizeof(long));
>                base=ptr;
> 
>                printf("\n in getRandomNumArray(long n)\n");
>                if(base!=NULL)
>                {
> 
>                        for(i=0;i<n;i++)
>                        {/* fread reads same value every time!*/
>                                fread(&val,sizeof(long),1,fp);
>                                printf("%ld: %ld\n",i,*val);
>                                *ptr++=*val;
>                        }
> 
>                }
>                else
>                {
>                        printf("Cannot Allocate memory\n");
>                        free(ptr);
>                        free(base);
>                        base=NULL;
>                        ptr=NULL;
>                }
>                fclose(fp);
> 
>        }
>        else
>        {
>                printf("Unable to open File...\n");
> 
>        }
>        return base;
> 
> }
> -------------------------------------------------------
> 
> apologies once again,
> help still needed...
> 
> thanks,
> -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!?
> Yahoo! Small Business - Try our new resources site!
> http://smallbusiness.yahoo.com/resources/
> 
> _______________________________________________
> The ILUGD-Dev mailing list
> Ilugd-dev-cunTk1MwBs8/[email protected]
> 
> Subscribe/Unsubscribe/Suspend your list membership at:
> http://frodo.hserus.net/mailman/listinfo/ilugd-dev
> 


Hi,
       In the code written by U, I found that in main U have declared
a pointer val which holds address of variable i. Therefore even though
the value of i changes in the loop the address of val never changes
and *val stores the last value held by variable i. Though U have
written the value of var in file temp.dat I believe the value of
variable var will always  be same and thereby the value of *var also
holding the last value of i i.e (10).

        Moreover, when U call the function getRandomNumArray(),
certain most compilers tend to release the variables stored in the
called function, thereby the value stored in those addresses may be
junk values, but this not always the case. It might just help if U
declared variable i as a global variable.

But if U want to save all the values of i I recommend U to save the
intermediate values of i and not its address.

Sorry for the delay, I was really busy and could'nt check my mail.

Hope it helps U,

Brijesh