• C

program to copy /dev/stdin to a file.

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,
CobolGuy9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dr_countryCommented:
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);

main(void)
{
     int result = OK;
     FILE *fd;
     char filename[] = "yourFile.txt";
     
     if( (fd = fopen(filename, "w")) == NULL )
     {
         printf("Error opening %s...\n", filename);
         result = FAILED;
     }
     
     writeUserInput(fd);
     fclose(fd);
     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';
     else
         while(getchar() != '\n')
         ;   //discard input until newline, then write line to file
         fputs(str, f);
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Artysystem administratorCommented:
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"
int
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;
}
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

CobolGuy9Author Commented:
thank you all,

need to incorporate tmpfile() because file needs not to be static and stdin length maybe 0-10kb
Artysystem administratorCommented:
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.
dr_countryCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.