[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

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,
0
CobolGuy9
Asked:
CobolGuy9
1 Solution
 
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);
}
0
 
NopiusCommented:
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;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CobolGuy9Author Commented:
thank you all,

need to incorporate tmpfile() because file needs not to be static and stdin length maybe 0-10kb
0
 
NopiusCommented:
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.
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now