Write/Read struct to file

A

a

I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
Then I want to read it out.
//file open
lseek(fd,0,SEEK_SET);
struct _structA B;
read(fd,B,sizeof(_structA));
//file close
Why I cannot get the correct value for the x,y,z?
Thanx
 
D

Daniel Fischer

I have a struct to write to a file
...
Why I cannot get the correct value for the x,y,z?

There are several parts of your code that aren't covered by the C
standard, and those that are lack compliance, and also address operators.


Daniel
 
F

Flash Gordon

a said:
I have a struct to write to a file
struct _structA{

Using a name starting with an underscore is a really bad idea. Most of
them are reserved for use by the implementation and do you know for a
*fact* that the specific one you are using is not reserved?
long x;
int y;
float z;
}
struct _structA A;
//file open

// style comments are only valid in C99 which most compilers do not
implement, although many support them as an extension in a
non-conforming mode. Also, they are a very bad idea on Usenet in genera;
since they don't survive line wrapping, although I will admit that is
unlikely to be a problem in this case.
write(fd,A,sizeof(_structA));

Why not use sizeof A so that it continues to be correct even if you
change the type of A?

write is not part of standard C, it might be part of your implementation
or a function you have written yourself, and for all we know it might
cause your system to use a robot arm to write the writing on the wall.
If it does something more useful it might return a value indicating
whether it succeeded or not.

We only deal with standard C here.
//file close
Then I want to read it out.
//file open
lseek(fd,0,SEEK_SET);

lseek is not a standard function.
struct _structA B;
read(fd,B,sizeof(_structA));

read is not a standard function either, although I would not be
surprised if it returns a value indicating whether it succeeded or not.
//file close
Why I cannot get the correct value for the x,y,z?

How would I know? Your code uses non-standard functions which we don't
deal with here and you have not provided a *complete* example showing
the problem, so the problem could be on any of the lines you have not
posted.

I suggest you either post a complete compilable example in *standard* C
here, or a complete compilable example in non-standard C somewhere it is
topical, such as comp.unix.programmer if your platform is a Unix like
system.
 
M

Martin Ambuhl

a said:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
Then I want to read it out.
//file open
lseek(fd,0,SEEK_SET);
struct _structA B;
read(fd,B,sizeof(_structA));
//file close
Why I cannot get the correct value for the x,y,z?

A good guess is that you needed to use pointers in the variable lists to
the nonstandard functions read() and write(). See the calls to the
standard fread() and fwrite() in the code below. Not only do you use
nonstandard functions, you have hidden possibly important parts of your
code.


/* I have renamed the structure tag, simply because identifiers with
leading underscores are reserved to the implementation in so many
places, it is best to avoid them rather than learning the rules for
when they are OK.

I have added the missing semicolon at the end of the definition of
struct structA.

Since your "code" is nowhere close to a compilable unit I have made
it one. Always try to post a small compilable unit to illustrate
your problem.

Since neither 'write' nor 'read' are standard C functions, I have
fixed these.

You do not show how you open the file. This may have something to
do with your problem. */

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

struct structA
{
long x;
int y;
float z;
};

int main(void)
{
const char fname[] = "./testfile";
FILE *fd;
size_t nio;

struct structA A = { 7L, 6, 5.2 }, B;

printf("The original structA A has elements\n"
" A.x = %ld (long), A.y = %d (int), A.z = %g (float)\n\n",
A.x, A.y, A.z);

printf("Using the testfile in binary mode.\n");
if (!(fd = fopen(fname, "wb"))) {
printf("Could not open \"%s\" for output, bailing...", fname);
exit(EXIT_FAILURE);
}
printf("Attempting to write the structure (its size is %lu).\n",
(unsigned long) sizeof A);
nio = fwrite(&A, sizeof A, 1, fd);
printf("%lu units written.\n"
"Attempting to close and reopen for input.\n",
(unsigned long) nio);
fclose(fd);
if (!(fd = fopen(fname, "rb"))) {
printf("Could not open \"%s\" for input, bailing...", fname);
exit(EXIT_FAILURE);
}
printf("Attempting to read the structure.\n");
nio = fread(&B, sizeof B, 1, fd);
printf("%lu units read.\n"
"The read structA B has elements\n"
" B.x = %ld (long), B.y = %d (int), B.z = %g (float)\n\n",
nio, B.x, B.y, B.z);
printf("Attempting to zero out B,");
B = (struct structA) {0L, 0, 0,}; /* you may need to replace this
with individual assignments or memset() */
printf("It now has elements\n"
" B.x = %ld (long), B.y = %d (int), B.z = %g (float)\n\n",
B.x, B.y, B.z);
fclose(fd);

printf("Using the testfile in text mode.\n");
if (!(fd = fopen(fname, "w"))) {
printf("Could not open \"%s\" for output, bailing...", fname);
exit(EXIT_FAILURE);
}
printf
("Attempting to write the structure (its size is %lu).\n",
(unsigned long) sizeof A);
nio = fwrite(&A, sizeof A, 1, fd);
printf("%lu units written.\n"
"Attempting to close and reopen for input.\n",
(unsigned long) nio);
fclose(fd);
if (!(fd = fopen(fname, "r"))) {
printf("Could not open \"%s\" for input, bailing...", fname);
exit(EXIT_FAILURE);
}
printf("Attempting to read the structure.\n");
nio = fread(&B, sizeof B, 1, fd);
printf("%lu units read.\n"
"The read structA B has elements\n"
" B.x = %ld (long), B.y = %d (int), B.z = %g (float)\n",
nio, B.x, B.y, B.z);
fclose(fd);
return 0;
}


The original structA A has elements
A.x = 7 (long), A.y = 6 (int), A.z = 5.2 (float)

Using the testfile in binary mode.
Attempting to write the structure (its size is 12).
1 units written.
Attempting to close and reopen for input.
Attempting to read the structure.
1 units read.
The read structA B has elements
B.x = 7 (long), B.y = 6 (int), B.z = 5.2 (float)

Attempting to zero out B,It now has elements
B.x = 0 (long), B.y = 0 (int), B.z = 0 (float)

Using the testfile in text mode.
Attempting to write the structure (its size is 12).
1 units written.
Attempting to close and reopen for input.
Attempting to read the structure.
1 units read.
The read structA B has elements
B.x = 7 (long), B.y = 6 (int), B.z = 5.2 (float)
 
A

Anand

a said:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
Then I want to read it out.
//file open
lseek(fd,0,SEEK_SET);
struct _structA B;
read(fd,B,sizeof(_structA));
//file close
Why I cannot get the correct value for the x,y,z?
Thanx

Welcome to c.l.c
http://www.ungerhu.com/jxh/clc.welcome.txt

You have a good selection of FAQs there..
http://www.eskimo.com/~scs/C-faq/top.html

For this particular question:
http://www.eskimo.com/~scs/C-faq/q2.11.html
 
J

Joe Wright

a said:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
Then I want to read it out.
//file open
lseek(fd,0,SEEK_SET);
struct _structA B;
read(fd,B,sizeof(_structA));
//file close
Why I cannot get the correct value for the x,y,z?
Thanx

You are not writing C. You have not exhibited an actual program.
Consider the following which I have named a.c ..

#include <stdio.h>

char file[] = "a.bin";

struct A {
long x;
int y;
float z;
};

int main(void) {
FILE *fp;
struct A A;
struct A B;
A.x = 80000;
A.y = 40000;
A.z = 1.2345;
printf("%ld %d %f\n", A.x, A.y, A.z);
fp = fopen(file, "wb");
fwrite(&A, sizeof A, 1, fp);
fclose(fp);
fp = fopen(file, "rb");
fread(&B, sizeof B, 1, fp);
fclose(fp);
printf("%ld %d %f\n", B.x, B.y, B.z);
return 0;
}

If you can glean anything from the above scribble, have at it. It is not
perfect (no error checking) and I am not paid to teach you C.
 
M

Mike Wahler

Joe Wright said:
If you can glean anything from the above scribble, have at it. It is not
perfect (no error checking) and I am not paid to teach you C.

And I am not paid to C you teach.

(Sorry, couldn't help myself) :)

-Mike
 
J

Joe Wright

Mike said:
And I am not paid to C you teach.

(Sorry, couldn't help myself) :)

I hope I did teach. Rather than poke holes in what other people do I try
to show what I would do and let them work it out.
 
M

Mike Wahler

Joe Wright said:
I hope I did teach. Rather than poke holes in what other people do

'Poking holes' is a legitimate teaching tool.
I try to show what I would do and let them work it out.

And that also is. As are many other things.

-Mike
 

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
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top