Buffer
Buffer is divided into two types:
- Fully buffered: it is emptied (content sent to destination) when the buffer is full
- Line Buffer: Buffer will be cleared when the encounter a newline character
Termination keyboard input
Files, streams, and keyboard input
Low-level I / O is to use the basic tools of the host operating system to process the file, due to differences in the system, it is not possible to establish a common low-level I / O standard library. Thus C is used in the standard I / O package , higher level, the difference between the system to handle a particular realization C, provides the equivalent of a standard interface
Conceptually, C program dealing with a stream rather than directly with files.
Stream (stream) is a data stream idealized actual input or output data stream mapped to the
C treat input and output device common thereto to treat the same file on the storage device, the keyboard input is referred to by a stream stdin said output on the screen is represented by the stdout stream
End of file
Detection end of the file in two ways:
- In the document places a special character to mark the end of the
- Allow the operating system to store file size information
Unified approach in C: Let getchar function returns the file when it reaches the end of a special value EOF, regardless of operating system how to detect the end of file
Redirection and file
Make programs and files to work with the way:
- Explicit use to open the file, close the file, read files, write files, and other special functions
- A program designed for use with a keyboard and screen work together, but use different channels to redirect input and output, is about to reassign stdin stream to a file
Input redirection
<
Unix is a redirection operator, and the file associations together stdin stream, the contents of the file to the program guide, the program itself does not care input is a keyboard or from a file
Output redirection
>
The display device redirection on stdout from a file, if the file already exists, usually delete the file and replace it with a new file
Redirect combination
note:
- The order is irrelevant redirection operator
- Do not use the same input and output file names for the same command
- Input and output files can only be a
- Only for a connection with another executable program data files
- Space is not required
Create a more user-friendly interface
Mixed enter numbers and characters
getchar and scanf not well mixed, since each character getchar read, while reading digital scanf will skip spaces, tabs, and newline
Input confirmation
When scanf input fails, the remaining input of the error in the input queue, this time can read the input character by character to getchar
A classic example of input validation:
int get_int(void){
int input;
char ch;
while(scanf("%d",&input)!=1){//如果输入不成功,则进入循环
while((ch=getchar())!='\n'){//getchar逐个读取输入的字符
putchar(ch);//剔除错误输入
}
}
return input;
}