Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Writing a structure into a file (http://www.go4expert.com/forums/writing-structure-file-t11600/)

mdzkhan 24Jun2008 20:02

Writing a structure into a file
 
I am using the following code
Code:

typedef struct A {
int x;
int y;
int  z;
};

int main(void) {

 struct A A;
 struct A B;
FILE *fp;
A.x = 80000;
A.y = 40000;
A.z = 12345;

printf("%d %d %d\n", A.x, A.y, A.z);
fp = fopen("file.txt", "wb");
fwrite(&A, sizeof A, 3, fp);
fclose(fp);
fp = fopen("file.txt", "rb");
fread(&B, sizeof B, 1, fp);
fclose(fp);
printf("%d %d %d\n", B.x, B.y, B.z);
return 0;
}

But the file.txt is containing junk values when i open it . Please let me know what is wrong in my program.

Thanks,
Zaheer

shabbir 24Jun2008 20:23

Re: Writing a structure into a file
 
Because you are writing it in binary mode.

Subrat_Mishra 28Jun2008 11:20

Re: Writing a structure into a file
 
Because you have assigned the value to int type variable beyond its range.


All times are GMT +5.5. The time now is 23:32.