program to copy /dev/stdin to a file.

Posted on 2006-04-22
Last Modified: 2008-02-01
hello all,

I need to copy stdin to a file.
in shell, bash,korn script I can do the following

var=$RANDOM ## random number for the file
 "tee -a /tmp/file-collected-fom-stdin-${var}.txt"

I want to do this in C, please give me an example program.
many thanks,
Question by:CobolGuy9
    LVL 45

    Expert Comment

    LVL 1

    Accepted Solution

    Hi CobolGuy9, so you want to write something from stdin to a file? I'm sure if this is what you need, but try it out. Hope it helps.

    #include <stdio.h>

    enum { OK, FAILED };
    void writeUserInput(FILE *f);

         int result = OK;
         FILE *fd;
         char filename[] = "yourFile.txt";
         if( (fd = fopen(filename, "w")) == NULL )
             printf("Error opening %s...\n", filename);
             result = FAILED;
         return result;

    void writeUserInput(FILE *f)
         char str[80];
         int length = 80;
         fgets(str, length, stdin);

         if( str[ (length = strlen(str) - 1) ] == '\n' )
              str[length] = '\0';
             while(getchar() != '\n')
             ;   //discard input until newline, then write line to file
             fputs(str, f);
    LVL 27

    Expert Comment

    sunnycoder, I don't see how dup() might help.

    dr_country is closer to the quesion, but 'binary' stdin will be broken.

    CobolGuy9, if you want to copy stdin to somewhere,

    read from file descriptor 0 (or STDIN_FILENO, defined in <unistd.h>)
    write to somewhere
    then write to stdout (STDOUT_FILENO constant)

    Here is the simplest 'tee.c' program without error checking:

    #include <unistd.h>
    #include <fcntl.h>
    #define DEFAULT_PATH "/dev/null"
    main(int argc, char *argv[])
      char buf[512];
      int fd, cnt;
      if (argc>1) fd=open(argv[1], O_WRONLY|O_CREAT);
      else fd=open(DEFAULT_PATH, O_WRONLY|O_CREAT);
      while(cnt=read(STDIN_FILENO, buf, sizeof(buf)))
        write(fd, buf, cnt),
        write(STDOUT_FILENO, buf, cnt);
      return 0;

    Author Comment

    thank you all,

    need to incorporate tmpfile() because file needs not to be static and stdin length maybe 0-10kb
    LVL 27

    Expert Comment

    CobolGuy9, may be you can describe entire problem?
    In very rare cases you need to create tmp files. Most unix progams may accept stdin as an input.
    LVL 1

    Expert Comment

    CobolGuy9, did you try running the program? It takes a variable length stdin, so you can type stuff and when you press 'enter' everything that was typed will be written to a temp file. Let me know if it works for you.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now