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)