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

Re: writing struct to disk with write()



Hi ,
 try this out instead  ========>  fd = open("aaa.info", O_WRONLY | O_APPEND);
do check out the man page for open() ====>  int open(const char *path,
int oflag, ... /* [mode_t mode] */ );... it says that  exactly one of
the O_RDONLY, O_WRONLY, or O_RDWR flags must be used in composing  the
value of oflag.
regards
manimay...!


On Thu, 26 Oct 2000, Arsalan Zaidi wrote:

> 
> Hi.
> 
> 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?
> 
> --Arsalan.
> 
> 
> 
> 
> #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;
> };
> 
> main()
> {
> struct file_info_struct file_info;
> int fd,  ret;
> char *msg = "azaidi password file.txt 1024";
> memset(&file_info,0,sizeof(file_info));
> 
> 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");
> close(fd);
> 
> printf("sizeof = %d\n", sizeof(file_info));
> printf("%s %s %s %d", file_info.username, file_info.password,
> file_info.filename,file_info.filesize);
> 
> }
> 
> 
> 
> ---------------------------------------------
> LIP is all for free speech.  But it was created
> for a purpose.  Violations of the rules of
> this list will result in stern action.
> 
>