Computing.Net > Forums > Programming > fopen & fread

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

fopen & fread

Reply to Message Icon

Name: eugensth
Date: January 7, 2006 at 01:39:36 Pacific
OS: Red Hat 8.0
CPU/Ram: AMD 450 Mhz 128 MB RAM
Comment:

Hi,

I dont't understand why happens this thing in C:after compiling and running
this little program:


struct var{
char nume[32];

int*valori;
};

main(){

FILE*p;

struct var test,test2;

test.valori=malloc(sizeof(int));

test.valori[0]=1;

p=fopen("\\date.sta","w");

fwrite(&test,sizeof(struct var),1,p);

fclose(p);

p=fopen("\\date.sta","r");


fread(&test2,sizeof(struct var),1,p);

printf("%d",test2.valori[0]);

fclose(p);

}

the output is this: 1 (as expected),
but if I split this code in 2 separate programs,like this
Program 1:

main(){

FILE*p;

struct var test;

test.valori=malloc(sizeof(int));

test.valori[0]=1;

p=fopen("\\date.sta","w");

fwrite(&test,sizeof(struct var),1,p);

fclose(p);
}

Program 2:

main(){

FILE*p;

struct var test;

test.valori=malloc(sizeof(int));

p=fopen("\\date.sta","r");

fread(&test,sizeof(struct var),1,p);

printf("%d",test.valori[0]);

fclose(p);

}

the output of program 2 is, instead of 1, the unexpected 4239900.
Can somebody explain this to me ?

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Stephen Hall
Date: January 8, 2006 at 05:26:47 Pacific
Reply:

eugensth,

You're not allocating more memory for the *valori data member in the second var structure. What you're doing is writing the value of the _pointer_ *valori in the test structure (a memory address) to the file and then reading it into the test2 *valori data member. It "works" if you don't start a new program because the memory location contains the same data since the program never stopped. If you split it up, the memory location is trashed in between the two apps and is therefore an unexpected value.

When you have pointers in structures, you can't just write the structure to the hard drive like,

fwrite(&test,sizeof(struct var),1,p);

Instead, you're going to have to write each data member seperately. Something like,

fwrite(test.nume,sizeof(char)*32,1,p);
fwrite(test.valori,sizeof(int),1,p);

would work. Be sure to change the size of the data in the second fwrite to reflect how much memory you allocated!

When reading, you can read the first part fine, but you'll have to allocate the *valori pointer before using it.

fread(test2.nume,sizeof(char)*32,1,p);
test2.valori=(int *)malloc(sizeof(int));
if(!test2.valori){exit(1);}
fread(test2.valori,sizeof(int),1,p);

Again, be sure to change the size of the data being read and allocated based on how much you wrote into the file. The 3rd line in the above code is something you need to do if you aren't doing it already. You can't assume malloc() will work and return valid memory. Check to see if the poiner equals NULL or 0. if(!ptr) is an easy way to do this (change ptr to the name of your pointer).

Hope this helps. Things like this can be confusing, but don't let it discourage you. You've got to remember that when you're using a pointer, it's a dynamically created array. However, unlike normal arrays, the compiler doesn't know how much room you've got stored in that pointer (if any). Therefore, you cannot use a simple fwrite to write a structure with a pointer in it. You must take care of writing the structure, member by member, yourself. At least in C/C++.

Good luck,

Stephen


"Live long and PROGRAM......or at least do _something_ with all that time...!"


0

Response Number 2
Name: eugensth
Date: January 8, 2006 at 08:48:54 Pacific
Reply:

Well, now I've fixed up the code and everything is OK.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: fopen & fread

Using fread with a large file www.computing.net/answers/programming/using-fread-with-a-large-file-/10254.html

fopen() equivalent in Win2k www.computing.net/answers/programming/fopen-equivalent-in-win2k/1027.html

Need help using PHP? PLEASE www.computing.net/answers/programming/need-help-using-php-please/9319.html