[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

writing struct to disk with write()


It's nearly 1:30 am and I'm not getting this... When I run this prog,
perror() tells me write() won't write because of a Bad File Descriptor.
open() however succeeds. What am I doing wrong?


#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <time.h>
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

struct file_info_struct
  char username[8];
  char password[10];
  char filename[256];
  long filesize;
  time_t timestamp;

struct file_info_struct file_info;
int fd,  ret;
char *msg = "azaidi password file.txt 1024";

sscanf(msg, "%s %s %s %d", file_info.username, file_info.password,
file_info.filename, &file_info.filesize);

fd = open("aaa.info", O_APPEND);
printf("error 1 = %d\n", errno);

perror ("after open");
//fd = open("/dev/pts/0",0);
printf("fd = %d\n",fd);

ret = write(fd, &file_info, sizeof(file_info));

printf("write = %d\n",ret);
printf("error 2 = %d\n", errno);
perror("write error");

printf("sizeof = %d\n", sizeof(file_info));
printf("%s %s %s %d", file_info.username, file_info.password,