Input output relocation in CC ++ program

xiaoxiao2021-03-06  43

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 #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;}

转载请注明原文地址:https://www.9cbs.com/read-68871.html

New Post(0)