MuthuGanapathy
MuthuGanapathy

Reputation: 49

C write() function not working

I am trying to write into a file, but it is not working. I can open a file, but while writing in the file using write function, tt is writting in the stdout itself, and the content of the file I opened remain unchanged.

#include<stdio.h>
#include<sys/file.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<limits.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/uio.h> 
main() {  
  char fn[30]; 
  int fd,i=0; 
  int actualbytes,bytesstored; 
  char buffer[100];

  printf("\nEnter the filename with path"); 
  scanf("%s",fn); 

  if(fd=open(fn,O_WRONLY|O_CREAT,S_IWUSR|S_IWUSR)<0) 
  {  
     perror("open"); 
     exit(0); 
  } 
  else 
  {
    write(stdout,"\n\nEnter the contents for the file\n");  
    write(stdout,"press CTRl+D at the end of the file\n\n");

    fflush(stdout); 
    while((buffer[i]=getc(stdin))!=EOF)  i++;

    buffer[i]='\0'; 
    bytesstored=sizeof(buffer); 

    if(actualbytes=write(fd,buffer,bytesstored)<0)
    {  
       perror("write");  
       exit(0); 
    } 
    else 
    { 
       write(stdout,"\n\nfile is opened successfully");

       write(stdout,"\nThe contents are written"); fflush(stdout); 
    } 
    if(close(fd)<0) 
    {  
       perror("close");  
       exit(0); 
    } 
    else
       printf("\nfile is closed"); 
 } 
}

Upvotes: 4

Views: 12326

Answers (5)

Damico
Damico

Reputation: 1227

just include and the warning will disappear.

Upvotes: -1

Morten Kristensen
Morten Kristensen

Reputation: 7613

As the others noted, there are a lot of problems with your code. Always instruct your compiler to show warnings. If you are using GCC then pass the argument -Wall to show all warnings. Now, if I do so with your code it suggests the following:

write.c:9: warning: return type defaults to ‘int’
write.c: In function ‘main’:
write.c:18: warning: suggest parentheses around assignment used as truth value
write.c:25: warning: implicit declaration of function ‘write’
write.c:34: warning: suggest parentheses around assignment used as truth value
write.c:45: warning: implicit declaration of function ‘close’
write.c:55: warning: control reaches end of non-void function

The first one means that your function main() defaults to int but you should always state a return type. On line 18 and 34 you need parentheses around the assignments before testing with < (as Ignacio said above). On line 25 and 45 it can't find the prototype for write() and close(), so you need to include the right header files. The last one means that you need to have a return statement (because it defaulted to type int).

Upvotes: 1

sarnold
sarnold

Reputation: 104020

Please note, from stdin(3):

   #include <stdio.h>

   extern FILE *stdin;
   extern FILE *stdout;
   extern FILE *stderr;

stdin, stdout, are standard IO FILE * streams, for use with fprintf(3), fgets(3), and so forth.

read(2) and write(2) take filedescriptors (which are represented as ints).

Keeping the C-supplied standard IO streams and the Unix-supplied filedescriptors separate in your mind is vital to sane Unix programming; sorry it's complicated :) but it's well worth becoming an expert.

I suggest changing all your write(stdout,... to fprintf(stdout,....

Ah, I see Ignacio has spotted the core problem :) it's hard to put one past him.

Another issue to worry about, your scanf() call doesn't limit the length of input to the size of your buffer. Someone could overflow your buffer and scribble data of their choosing all over memory. It's not a big deal when you're learning, but this kind of bug is exactly how the first Internet worm infected some new machines, so it is well worth not making the same mistake again.

And the last issue I spotted is how you're writing out your buffer:

buffer[i]='\0';
bytesstored=sizeof(buffer); 
if(actualbytes=write(fd,buffer,bytesstored)<0)

sizeof(buffer) is always going to return 100, because that is what you declared for buffer at the start of your program. So replace with this:

buffer[i++]='\0';
if(actualbytes=write(fd,buffer,i)<0)

Upvotes: 2

Mat
Mat

Reputation: 206659

There are a lot of problems with this code. Make sure you enable warnings on your compiler, it should complain about quite a few things:

write() is in unistd.h. You're not including that, so your program cannot be correct. Once you include that, you'll notice (with warnings enabled), that you're calling it incorrectly at least 5 times: stdout is not a file descriptor, it's a FILE*.

Use the printf() family of functions to print things on the console.

Second big problem is your if statements that have assignments in them.

if (a = b < 0) { ... }

is equivalent to:

if (a = (b < 0)) { ... }

So it's not doing what you think it is. You need to use parenthesis:

if ((fd = open(...)) < 0) { ... }

Note: you're always writing the full buffer to the file. Not all of it has been initialized. That doesn't sound like what you're after. Try only writing the data that you've read (you have that stored in i).

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

< has higher precedence than =.

if((fd=open(fn,O_WRONLY|O_CREAT,S_IWUSR|S_IWUSR))<0)

Upvotes: 3

Related Questions