0

I am writing a program in C that takes a a command line argument that represents the name of an output file. I then opened the file to write to it. The problem is that when I write something in the command line, it never shows up in the file I was writing to and any text in the file is erased. Here is the code I have for writing to the file from stdin. (fdOut is the FILE * stream that was specified)

 while(fread(buf, 1, 1024, stdin))
 {
   fwrite(buf, 1, 1024, fdOut);
 }
Kalmar
  • 195
  • 2
  • 18
  • There is a good chance that the error is elsewhere. Can you show us more code ? For example, did you open ``fdOut`` properly ? – hivert Jul 22 '13 at 22:03
  • 3
    Note that `stdin` is not "the command line". The command line is only what you use to start the program. I'm assuming you're trying to write something like `cat` that reads from `stdin` but writes to a file? – Jonathon Reinhart Jul 22 '13 at 22:04

3 Answers3

2

try this code.

#include "stdio.h"

int main()
{
        char buf[1024];
        FILE *fdOut;
        if((fdOut = fopen("out.txt","w")) ==NULL)
        {       printf("fopen error!\n");return -1;}
        while (fgets(buf, 1024, stdin) != NULL)
        {
            //   int i ;
            //   for(i = 0;buf[i]!=NULL; ++i)
            //          fputc(buf[i],fdOut);
                 fputs(buf,fdOut);
            //   printf("write error\n");
        }
        fclose(fdOut);
        return 0;
}

Note : use Ctrl+'D' to stop input.

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • That worked! The only problem is that the error checking for fopen doesn't seem to be working right. If I use a nonexistent file, it will create the file and then write to it. – Kalmar Jul 22 '13 at 22:58
  • Why not `fputs(buf, fdOut)`? Note that this will copy text OK; it will not copy files that contain zero bytes (such as UTF-16 files) accurately. – Jonathan Leffler Jul 22 '13 at 23:49
  • Also, good point about using Control-D to end the input. Using Control-C (interrupt) would leave the file empty unless you'd typed multiple lines of input. – Jonathan Leffler Jul 23 '13 at 00:19
  • @JonathanLeffler I use the fputs() function the first time,but it write a extra '\n' after each input.I don't know why... – Lidong Guo Jul 23 '13 at 02:37
  • No: [`puts()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html) outputs a newline after the data; [`fputs()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fputs.html) does not. – Jonathan Leffler Jul 23 '13 at 02:44
  • @katherine When we open the file with flag "w",it will create the file if it does not exist .So the error checking is excess ,but it's not a error. – Lidong Guo Jul 23 '13 at 05:30
0

let's assume that there is data coming in at stdin, d.g. you are using your program like:

 cat infile.txt | ./myprog /tmp/outfile.txt

then data written with fwrite() will be buffered, so it won't appear immediately in the output file, but only when your OS decides that it's time to flush the buffer. you can manually force writing to disk by using

 fflush(fdOut);

(probably you don't want to do this all the time, as buffering allows for great speedups, esp when writing to slow media)

umläute
  • 28,885
  • 9
  • 68
  • 122
  • Note that it's not your OS that decides when to flush the buffer. It's `libc` who is buffering output data, and it is usually *line-buffered* which means that when a newline is written, it actually calls `write` to flush the buffers. – Jonathon Reinhart Jul 22 '13 at 22:17
0
size_t nbytes;
while ((nbytes = fread(buf, 1, 1024, stdin)) > 0)
{
    if (fwrite(buf, 1, nbytes, fdOut) != nbytes)
        ...handle short write...out of space?...
}

As you wrote it, you mishandle a short read, writing garbage that was not read to the output.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278