How to take input from cmd line into file

Hi, I want to be able to write a simple program that takes in input from the command line. I;m am at the level of getchar and putchar. Any examples would be a great help thanks.

I intend/prefer also to use the pipe command | eg: input | file1

---------- Post updated at 04:08 PM ---------- Previous update was at 04:01 PM ----------

C is the programming language I am using

man popen

simple example:

 
FILE *pfp;
if((pfp = popen("ls", "r")) != NULL)
{
    while(fgets(bufffer, sizeof(buffer)-1, pfp))
    {
        printf("I read: %s\n", buffer);
    }
    pclose(pfp);
}

Hope that this is the simplest input output program in C , using getchar and putchar

#include<stdio.h>
main()
{
        int c ;
        while((c=getchar())!=EOF)
                putchar(c);
}

For what its worth, check out the readline library. It is somewhat more work than getc/putc, but in many many cases its very much worth the effort: you get command line editing, command line history, key bindings, etc etc, and the API is not that difficult to use:

  char line[MAX_LINE_LEN];

  line = readline ("prompt > ");
 
  /* If the line has any text in it, save it on the history. */
  if (line && *line)
      add_history (line);

The GNU Readline Library has all the details you want, and then some :wink:

Hi,

Using following program you can get the input from the command line
and print that line in stdout.

#include<stdio.h>
#define BUFSIZE 1024

main()
{
        char string[BUFSIZE];
        scanf(" %[^\n]",string);
        printf("STRING:%s\n",string);

}