By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Writing a structure into a file

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
za*******@gmail.com wrote:
I am using the following code

Don't forget <stdio.h>
typedef struct A {
struct A A;
struct A B;
fwrite(&A, sizeof A, 3, fp);
What's the (3) for?
You don't have three A's

fread(&B, sizeof B, 1, fp);
But the file.txt is containing junk values when i open it . Please let
me know what is wrong in my program.

--
pete
Jun 27 '08 #2

P: n/a
On Jun 24, 7:36 pm, pete <pfil...@mindspring.comwrote:
zaheer...@gmail.com wrote:
I am using the following code

Don't forget <stdio.h>
typedef struct A {
struct A A;
struct A B;
fwrite(&A, sizeof A, 3, fp);

What's the (3) for?
You don't have three A's
fread(&B, sizeof B, 1, fp);
But the file.txt is containing junk values when i open it . Please let
me know what is wrong in my program.

--
pete
Sorry for that it should have been 1
Jun 27 '08 #3

P: n/a
za*******@gmail.com wrote:
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 <somethingNewTypeName;

Your <somethingis 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
#includeing <stdio.h>, the compilation worked fine and the program
output was the expected one ... but see below
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 */
}
Jun 27 '08 #4

P: n/a
za*******@gmail.com wrote:
On Jun 24, 7:36 pm, pete <pfil...@mindspring.comwrote:
>zaheer...@gmail.com wrote:
>>I am using the following code
Don't forget <stdio.h>
>>typedef struct A {
struct A A;
struct A B;
fwrite(&A, sizeof A, 3, fp);
What's the (3) for?
You don't have three A's
>>fread(&B, sizeof B, 1, fp);
But the file.txt is containing junk values when i open it . Please let
me know what is wrong in my program.
--
pete

Sorry for that it should have been 1
My second problem is that this is the output on my machine:

C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
80000 40000 12345
80000 40000 12345

C:\Program Files\DevStudio\SharedIDE\bin\Debug>

Intermittent problems tend to be a little tougher to diagnose.

I convert the program to a correct C program and see what turns up.

--
pete
Jun 27 '08 #5

P: n/a
ba******@gmail.com wrote:
za*******@gmail.com wrote:
>fp = fopen("file.txt", "rb");
Why don't you write to the file in text mode
That's the first thing that popped into my mind.

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct A {
int x;
int y;
int z;
};
struct A A;
struct A B = {0};
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", "w");
if (fp == NULL) {
puts("fp == NULL #1");
exit(EXIT_FAILURE);
}
fprintf(fp, "%d %d %d\n", A.x, A.y, A.z);
fclose(fp);
fp = fopen("file.txt", "r");
if (fp == NULL) {
puts("fp == NULL #2");
exit(EXIT_FAILURE);
}
if (fscanf(fp, "%d %d %d", &B.x, &B.y, &B.z) != 3) {
puts("fscanf problem");
fclose(fp);
exit(EXIT_FAILURE);
}
fclose(fp);
printf("%d %d %d\n", B.x, B.y, B.z);
return 0;
}

/* END new.c */

--
pete
Jun 27 '08 #6

P: n/a
za*******@gmail.com wrote:
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;
This indicates that you've included <stdio.h>, which is a
good thing, but that you've omitted the inclusion from what
you've posted. What else did you leave out? Is the code I'm
trying to debug in any way to the code that's actually giving
you problems?
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");
Did the fopen() call succeed? Probably, or you'd most
likely see a different sort of error. Still, it *never* hurts
to check.
fwrite(&A, sizeof A, 3, fp);
Even under the most favorable of circumstances, your program
goes off the rails here and its behavior becomes unpredictable.
You have asked fwrite() to output the contents of three consecutive
`struct A' instances, but only one instance exists at the memory
location you've indicated. When fwrite() trustingly tries to output
the other two, anything can happen.

Oh, yes: Did the fwrite() succeed or fail, or succeed partially?
Again, it wouldn't hurt to check ...
fclose(fp);
Success? Failure?
fp = fopen("file.txt", "rb");
Success? Failure?
fread(&B, sizeof B, 1, fp);
Success? Failure? This time, at least, you're not asking for
more data than you've got memory to store it in.
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.
Fix the obvious blunder in fwrite(), add success/failure
checks, re-run, and come back again if you're still having
trouble.

--
Er*********@sun.com
Jun 27 '08 #7

P: n/a
pete wrote:
za*******@gmail.com wrote:
>On Jun 24, 7:36 pm, pete <pfil...@mindspring.comwrote:
>>zaheer...@gmail.com wrote:
I am using the following code
Don't forget <stdio.h>

typedef struct A {
struct A A;
struct A B;
fwrite(&A, sizeof A, 3, fp);
What's the (3) for?
You don't have three A's

fread(&B, sizeof B, 1, fp);
But the file.txt is containing junk values when i open it . Please let
me know what is wrong in my program.
--
pete

Sorry for that it should have been 1

My second problem is that this is the output on my machine:

C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
80000 40000 12345
80000 40000 12345
Is it the case that by "when i open it"
you are referring to a text editor,
and not the output of your program?

If so, then that's kind of what binary mode is all about.
There's no way that only knowing this program's source code,
will tell you which bytes are going to wind up in that file.

--
pete
Jun 27 '08 #8

P: n/a
On Jun 24, 7:13 pm, "zaheer...@gmail.com" <zaheer...@gmail.comwrote:
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.

Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.