user2742399
user2742399

Reputation: 71

unable to write into file using c , with unix system calls

I am working on unix system calls. I want to read the string from standard input using read() and then write it to the file using write().

I am able to open the file , read the string from standard input , but unable to write it to the file.

my code is :

#include <unistd.h>     // to remove WARNINGS LIKE  warning: implicit declaration of       function ‘read’ , warning: implicit declaration of function ‘write’
#include<fcntl.h>         /* defines options flags */
#include<sys/types.h>     /* defines types used by sys/stat.h */
#include<sys/stat.h>      /* defines S_IREAD & S_IWRITE  */
#include<stdio.h>

int main(void)
 {
 int fd,readd;
 char *buf[1024]; 


    fd = open("myfile",O_RDWR);
    if(fd != -1)
         printf("open error\n");
    else
    {
        // read i\p from stdin , and write it to myfile.txt

            if((readd=read(0,buf,sizeof(buf)))<0)
              printf("read error\n");
            else
             {
                    printf("\n%s",buf);
                    printf("\n%d",readd);
                if(write(fd,buf,readd) != readd)
                      printf("write error\n");

              }
    } 

return 0;
}

the output is

    write error

it is working properly , if I write the string to standard output

Question :

1) what is the problem with write() ?

2) I want to include newline character \n at the end of the line. How is it possible through standard input?

Upvotes: 0

Views: 2320

Answers (2)

nos
nos

Reputation: 229058

fd = open("myfile",O_RDWR);

This opens an existing file. If the file does not exist, you get an error. You can use perror() to get more descriptions of the error.

fd = open("myfile",O_RDWR);
if (fd == -1) {
   perror("open failed");
   exit(1);
}

The error here is that your error checking logic is wrong.

if(fd != -1)
     printf("open error\n");

Should be

if(fd == -1)
     printf("open error\n"); //or better yet, perror("open error");

Once that is corrected, you will still get an error when opening the file if the file does not already exist. To also create the file, you need an additional flag, and give it proper permissions:

fd = open("myfile",O_RDWR|O_CREAT, 0664);

Upvotes: 3

Art
Art

Reputation: 20392

if(fd != -1)
     printf("open error\n");

This doesn't look right. If your output isn't "open error", then it probably means that your call to open failed, because you only attempt write to the file when you've failed to open it.

A good idea is to print the errno when printing errors, print the errors to stderr, not stdout and exit immediately on error. Try using perror for printing error messages.

Also, I don't like this comment:

#include <unistd.h>     // to remove WARNINGS LIKE  warning: implicit declaration of       function ‘read’ , warning: implicit declaration of function ‘write’

The include isn't needed to "remove warnings". It's needed to make your program correct.

Upvotes: 2

Related Questions