Today a friend let me write a small program, which has a function: When specifying the file name, read the data from the file name; when the file name is not specified, the data is read from the standard input. Usually often use UNIX type operating systems, which is very familiar with this input data (such as: sort command). But the procedure is still written for the first time. This is used in the input and output repositioning, such as LS> Dirdata, this is to redirect the contents of Stdout to the file. It is also very easy to use program implementation. #include
Void Useage (const char * buf) {fprintf (stderr, "users:% s [file]", buf); exit (1);}
INT Main (int Argc, char * argv []) {Int a; file * fp; if (argc> 2) Useage (argv [0]);
IF (argc == 2) {fp = fopen (argv [1], "r"); if (fp == null) {PERROR ("*** error"); exit (1);} * stdin = * FP;} scanf ("% d", & a); Printf ("THE INPUT IS% D / N", A); Fclose (FP);
Return 0;}