C Primer Plus 学习笔记——第八章 Character Input/Output and Input Validation

C Primer Plus 学习笔记——第八章 Character Input/Output and Input Validation

  • 知识点整理
    • 1. Single-Character I/O: getchar() and putchar()
    • 2. Buffers
      • (1) unbuffered (or direct) input
      • (2) buffered input
        • 1) fully buffered I/O
        • 2) line-buffered I/O
    • 2. Files, Streams, and Keyboard Input
      • (1) file
      • (2) stream
    • 3. The End of File
    • 4. Key Concepts
    • 5. Summary

书籍:C Primer Plus第六版英文版
 

知识点整理

1. Single-Character I/O: getchar() and putchar()

getchar() and putchar() perform input and output one character at a time.
All it does is fetch characters from keyboard input and send them to the screen. This process is called echoing the input.

2. Buffers

(1) unbuffered (or direct) input

The characters you type are immediately made available to the waiting program.

(2) buffered input

The characters you type are collected and stored in an area of temporary storage called a buffer. Pressing Enter causes the block of characters you typed to be made available to your program.

1) fully buffered I/O

The buffer is flushed (the contents are sent to their destination) when it is full. This kind of buffering usually occurs with file input.
The buffer size depends on the system, but 512 bytes and 4096 bytes are common values.

2) line-buffered I/O

The buffer is flushed whenever a newline character shows up. Keyboard input is normally line buffered, so that pressing Enter flushes the buffer.

2. Files, Streams, and Keyboard Input

(1) file

A file is an area of memory in which information is stored.
Conceptually, the C program deals with a stream instead of directly with a file.

(2) stream

A stream is an idealized flow of data to which the actual input or output is mapped.

3. The End of File

A computer operating system needs some way to tell where each file begins and ends.

One method to detect the end of a file is to place a special character in the file to mark the end.

A second approach is for the operating system to store information on the size of the file.

C handles this variety of methods by having the getchar() function return a special value when the end of a file is reached, regardless of how the operating system actually detects the end of file. The name given to this value is EOF (end of file).

Therefore, the return value for getchar() when it detects an end of file is EOF. The scanf() function also returns EOF on detecting the end of a file.

Typically, EOF is defined in the stdio.h file as follows:

#define EOF (-1)

Why -1? Normally, getchar() returns a value in the range 0 through 127, because those are values corresponding to the standard character set, but it might return values from 0 through 255 if the system recognizes an extended character set.

In either case, the value -1 does not correspond to any character, so it can be used to signal the end of a file.
 
** The echo_eof.c Program**

/* echo_eof.c -- repeats input to end of file */
#include 
int main(void)
{
int ch;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}

Note these points:
C Primer Plus 学习笔记——第八章 Character Input/Output and Input Validation_第1张图片C Primer Plus 学习笔记——第八章 Character Input/Output and Input Validation_第2张图片

4. Key Concepts

Input and output involve functions, data, and devices.

C programs see input as a stream of incoming bytes. The getchar() function interprets each byte as being a character code. The scanf() function sees input the same way, but, guided by its conversion specifiers, it can convert character input to numeric values.

5. Summary

Many programs use getchar() to read input character-by-character. Typically, systems use linebuffered input, meaning that input is transmitted to the program when you press Enter.

Pressing Enter also transmits a newline character that may require programming attention.

C features a family of functions, called the standard I/O package, that treats different file forms on different systems in a uniform manner.

The getchar() and scanf() functions belong to this family. Both functions return the value EOF (defined in the stdio.h header) when they detect the end of a file.

你可能感兴趣的:(#,C,Primer,Plus)