UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-02-07 06:13 AM

usha
Member
Registered: 2005-11-16
Posts: 32

Re: files

hi
  here i am giving a code in my program.
   

#include<fcntl.h>
   #include<unistd.h>
   #include<sys/types.h>
   #include<sys/stat.h>

   main()
   {
      int fd;
      char ch[30];
      fd = open("SUCHINFO",O_CREAT|O_RDWR);
      write(fd,"U R IN SUCHI",12);
      lseek(fd,-12,SEEK_CUR);
      read(fd,&ch,12);
      write(1,&ch,12);
      close(fd);
   } 

~


   I have opended a file written a sentence and then printing. But when i open the file i could not find the matter which i have written. On the output its showing only for the first time. next when i run the same program it is showing garbage values. pls correct my program
                                                                    thanking u sir
                                                                           usha


[i]usha[/i]

Offline

#2 2006-02-07 01:05 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: files

Well, for one thing, your open() is incorrect...  If you specify O_CREAT, you must
supply a third arg giving the permissions to apply to the newly created file...  So,
it's my guess that the file is getting created with some bizarre set of perms (whatever
random crap it's pulling off the stack for that missing third arg), which prevents you
from opening it in the future...  And, since that code doesn't have ANY sort of error
checking on any of the functions, you wouldn't notice that was the problem, since the
code would just blindly stumble on...  You might want to think about checking each
syscall for failure, and reporting a helpful error message with errno and such; it would
make situations like this a lot easier to catch yourself...

Offline

Board footer

Powered by FluxBB