Writing a structure into a file

Z

zaheer031

I am using the following 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
 
C

Chris Dollin

I am using the following code

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

Pointless `typedef`. Either remove the `typedef` or
give it a name and use it.
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);

(`1`, not `3`; you're accessing store outside A. Bad news.)
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 .

Open it /how/?

If you're looking at it in a text editor, naturally it will
look like gibberish. It's a binary file. If you want your
text file to contain texty things, don't use `fwrite`; use
`fprintf`.
 
B

badc0de4

I am using the following code

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

Either provide a name for the "new type", or (better, I think) don't
typedef.

typedef <something> NewTypeName;

Your <something> is the whole struct A above. You do not have a name
for the 'new type'.
My compiler emitted a warning for the construction without a name for
the typedef:
"warning: useless storage class specifier in empty declaration"

After removing the typedef, keeping only "struct A { ... }" and
the compilation worked fine and the program said:
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");

What if "file.txt" cannot be opened? You should test fp.
if (fp == NULL) { /* file.txt could not be opened for writing */ }
fwrite(&A, sizeof A, 3, fp);

Now you're trying to access 3 elements of size "sizeof A".
You have no guarantee that the second element (or the third) can be
read.
fclose(fp);
fp = fopen("file.txt", "rb");

Verify that the fopen() call didn't fail.
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 .

What do you mean 'junk values'?
If your program didn't fail at writing 3 (inexistent) elements at the
fwrite() call, file.txt will have 3 * sizeof(struct A) bytes.
On my computer that is 3 * 12 bytes = 36 bytes.
ON MY COMPUTER, the first 12 bytes of the file correspond to the
members x, y, and z.
I have absolutely no idea what the other 24 bytes are.
Please let me know what is wrong in my program.
1) Failure to #include <stdio.h>
2) accessing inexistent data
3) writing more than you'd like to the file
4)? Misinterpretation of 'junk values'?

Why don't you write to the file in text mode using fprintf? and read
with fgets()?

fprintf(fp, "struct A: x = %d; y = %d; z = %d\n", A.x, A.y, A.z);

and

#define MAXLINE_LENGTH 80

char buf[MAXLINE_LENGTH];

if (fgets(buf, sizeof buf, fp) != NULL) {
/* parse buf to extract B.x, B.y, and B.z */
}
 
R

rahul

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

with stdio.h included, I am getting the output you expected.
80000 40000 12345
80000 40000 12345
Your issue is already addressed. Your code appears to be broken in
more than one ways but the issue of seeing junk is that you are using
a text editor to analyze the file. Use a binary/hexadecimal editor and
you can check that you are getting the right values and some junk
because of your 3 in fwrite. (If you haven't used any binary/hex
editor, it will be all greek and latin with all the endian-ness and
stuff to worry about)

My compiler warns about your typedef:

bin.c:10: warning: useless storage class specifier in empty
declaration

Using a static code checker can let you know other issues which you
are ignoring ( not checking the return value for fopen, fread, fwrite
etc). In my case, lint has got this to say.

Splint 3.1.1 --- 19 Jul 2006

bin.c: (in function main)
bin.c:25:28: Possibly null storage fp passed as non-null param:
fwrite (..., fp)
A possibly null pointer is passed as a parameter corresponding to a
formal
parameter with no /*@null@*/ annotation. If NULL may be used for
this
parameter, add a /*@null@*/ annotation to the function parameter
declaration.
(Use -nullpass to inhibit warning)
bin.c:24:8: Storage fp may become null
bin.c:25:3: Return value (type size_t) ignored: fwrite(&A, sizeo...
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalother to inhibit
warning)
bin.c:26:3: Return value (type int) ignored: fclose(fp)
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalint to inhibit
warning)
bin.c:28:27: Possibly null storage fp passed as non-null param: fread
(..., fp)
bin.c:27:8: Storage fp may become null
bin.c:28:3: Return value (type size_t) ignored: fread(&B, sizeof...
bin.c:29:3: Return value (type int) ignored: fclose(fp)


lint is not related to the language C, but the output I have posted is
related to your C problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top