I'm trying to pass an array of structures to the *nix read() function. Here is the code...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
struct bbs {
char *s;
int i;
};
struct bbs user[1];
int main(void)
{
user[0].s = "chad\n";
user[0].i = 1;
/*user[1].s = "was\n";
user[1].i = 1;
user[2].s = "here\n";
user[2].i = 1;
*/
ssize_t n;
int fd;
if ((fd = open("/home/cdalten/oakland/ho", O_RDWR)) < 0) {
perror("Can't open file\n");
exit(EXIT_FAILURE);
}
write(fd, &user, sizeof(struct bbs));
while((n = read(fd, &user, sizeof(struct bbs))) > 0 ) {
printf("The string is: %s\n", user[1].s);
}
close(fd);
exit(EXIT_SUCCESS);
}
And here is what I get when I compile and run the code.
[cdalten@localhost oakland]$ gcc -g -Wall wr.c -o wr
[cdalten@localhost oakland]$ ./wr
The string is: (null)
The string is: (null)
[cdalten@localhost oakland]$