0

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?

lulyon
  • 6,707
  • 7
  • 32
  • 49
user2742399
  • 61
  • 2
  • 6
  • 3
    Instead of `char *buf[1024]` you probably want `char buf[1024]`. – cnicutar Sep 06 '13 at 10:55
  • 2
    Check the `errno` variable. – HAL Sep 06 '13 at 10:56
  • Instead of `printf("write error")`, use something like `perror("write")`. That shows you which error occurred. – Fred Foo Sep 06 '13 at 10:57
  • it gives me the error `Bad file descriptor` , which means these `file descriptor` is not valid , but what to do sir? I am not giving the `file descriptor`. – user2742399 Sep 06 '13 at 11:04
  • all the things mentioned below , is corrected . but it still not writing what I write in `standard input` , It writes `kkcmvdksvdslvdvldzvd`. – user2742399 Sep 06 '13 at 11:13
  • As a side note: You are not using any system calls here, only the C standard library (See [here](http://stackoverflow.com/questions/2668747/system-call-vs-function-call)). And please don't format `kind-of-technical-sounding-terms` like `code`, it's makes it very hard to read. – firefrorefiddle Sep 06 '13 at 11:26

2 Answers2

3
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);
nos
  • 223,662
  • 58
  • 417
  • 506
2
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.

Art
  • 19,807
  • 1
  • 34
  • 60