C language - the input and output control program set the parameters

A control program input and output
gadgets c language, the operating system will assign three data streams: a standard input, standard output and standard error output.
1. The user can write code to the control input of the output data stream number
2. The user can set the input and output through the operating system command target is a screen or a file, document or keyboard
1.c language standard input and output (standard error)

fprintf (stdout, " XXXX " ); 
equivalent to the printf ( " XXXX " );

stdout is the standard output stream
when the standard error output stream stderr

fscanf(stdio, "xxxx"x &x);
等价于scanf("xxxx", &x);

stdio standard input data stream

2. A number of control input and output data stream

FILE * name of the data stream = fopen ( " name of input file " , " R & lt " ); 
the FILE name * data stream = fopen ( " name of input file " , " W " ); 
the FILE name * data stream = fopen ( " enter the name of the file " , " a " );

fopen () function creates a new data flow
w, r, a, is the parameter corresponding to the read, write, append

Run data stream needs to close

fclose (name of the data stream);

A process up to 256 data stream

The control input and output with the target operating system command
Example 1.c written language gadget Linux (class unix) using the above system to define the data stream input and output redirection file error output file

./ gadget <need to enter file> output file name 2 > standard error output file (without this one, the error output will be output to the screen)

C procedures gadget, the return value standard error output is 2, the two lead into the standard error

Examples of standard input and standard output gadget by duct 2. The plurality of symbol c language connection

(Program 1 | 2 program) <input file> output file

II. Write code allows the user to set the parameters and options

1. Write code that allows the user to set the parameters
allow the user to set the output file name of the command and the parameters

int main (int xxx, char * xxx[])

The main function to increase the parameter, the name of the file to receive user input parameters and
where xxx is the variable name

2. Write code so that users can set options
library function getopt () uses
the function header file: # include <unistd.h>
write code so that the user can set parameters

getopt (number of parameters received by the keyboard, the keyboard received string argument, " XX: " )

x represents options
: on behalf of that option has parameters, this parameter defaults to point optarg

Also you need to set

xxxx -= optiond;
xxxx += optiond;

Where xxx is the variable name

Guess you like

Origin www.cnblogs.com/renren-study-notes/p/11622038.html