How to write the struct array into a file and read the second one? read always produces incorrect parameters.
Source: Internet
Author: User
How to write the struct array into a file and read the second one? Now, read always produces a wrong parameter error-Linux general technology-Linux technology and application information. The following is a detailed description. # Include
# Include
# Include
# Include
# Include
# Define BIZ_SIZ 100
# Define FPATH "./students"
Typedef struct
{Char name [10];
Int num;
} DATE;
Int swrite (int n, DATE p [])
{Int x, I = 0;
If (-1 = (x = write (n, p, sizeof (DATE) * 3 )))
{Perror ("write ");
Return-1;
}
Return x;
}
Int sopen (void)
{
Int k;
K = open (FPATH, O_RDWR | O_CREAT | O_TRUNC, 0666 );
If (-1 = k)
{
Perror ("open ");
Return-1;
}
Printf ("File creat sucess! : % D \ n ", k );
Return k;
}
Int sread (int n)
{DATE buf;
Int m;
Lseek (n, SEEK_SET, sizeof (DATE ));
If (0 = (m = read (n, buf, sizeof (DATE ))))
{Perror ("read ");
Return-1;
}
Printf ("% s \ n", buf. name );
Printf ("% d \ n", buf. num );
Return m;
}
Int main (int argc, char * argv [])
{
DATE a [3] = {"xiaomi", 9527 },{ "liaoli", 9528 },{ "siaoer", 9529 }};
Int n;
Char buff [BIZ_SIZ] = {0 };
Printf ("% d \ n", sizeof (DATE ));;
Printf ("% d \ n", sizeof ());
N = sopen ();
Printf ("% d BYTES was write \ n", swrite (n, ));
Printf ("The 2nd struct is: % d \ n", sread (n ));
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.