C language learning (11) character input/output


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

getchar() and putchar() process one character at a time.

getchar() is to receive characters from the keyboard. And putchar is to print out the received characters.

#include <stdio.h>


int main(void){
    
    

	char ch;
	// 将接收的字符赋值给ch,如果ch不等于#,则继续输入。
	// 如果有#,则#后面的内容不会被打印出来。
	while ((ch = getchar()) != '#')
	{
    
    
		putchar(ch);
	}	
	return 0;
}

Two, the buffer zone

2.1 What is a buffer

Printing the character repeatedly immediately after the character entered by the user is an unbuffered (direct) input , that is, the waiting program can use the input character immediately.

Most systems will not repeatedly print the characters just entered until the user presses the Enter key. This form of input is buffered input . The characters entered by the user are collected and stored in a temporary storage area called a buffer . Press Enter After pressing the key, the program can use the characters entered by the user.


2.2 Why do we need a buffer

When we make a mistake or type a wrong character, we can directly delete or go back through the delete key on the keyboard, and then re-enter. When the Enter key is finally pressed, the correct character is entered. If there is no buffer, there is no way to modify it.

While buffers have many benefits, some programs also require unbuffered input. So, both buffered and unbuffered inputs are needed.


2.3 Buffer classification

Buffering is divided into two categories: fully buffered I/O and line buffered I/O .

2.3.1 Fully buffered I/O

Fully buffered input means that the buffer is flushed (the content is sent to the destination) when the buffer is full, usually in file input. The size of the buffer depends on the system, common sizes are 512 bytes and 4096 bytes.


2.3.2 Line buffered I/O

Line-buffered I/O refers to flushing the buffer when a newline character occurs. Keyboard input is usually line-buffered, so the buffer is not flushed until the Enter key is pressed.


3. End keyboard input

In the above program, as long as there is no # in the input character, the program will always print out the content before #, until it reads #, the program will stop printing and outputting. But sometimes we have to use the # sign, so the requirements cannot be met at this time. Input should be marked complete with a character that is not used in the text so that such a character does not inadvertently appear among the characters entered. C provides such characters, but you need to understand the way C handles files.


3.1 Files, streams and keyboard input

A file is an area in memory that stores information. Usually, files are stored in permanent storage (for example: hard disk, USB flash drive, etc.). There is no doubt that files are very important to computer systems. The C program we write is saved in the file.

C handles files through standard I/O packages. This involved creating a standard model and a standard set of I/O functions for working with files.

C programs deal with streams rather than files directly. A stream is an idealized data flow of a map of actual input or output. This means that different kinds of inputs with different attributes are represented by a more uniform stream of attributes. Therefore, the process of opening a file is to associate the stream with the file, and both reading and writing are done through the stream.


3.2 End of file

The end of the file is for the computer operating system to somehow determine the beginning and end of the file.

One way is to put a special character at the end of the file to mark the end of the file (for example: CP/M, IBM-DOS, and MS-DOS text files have used this method). Another way is to store the file size information. If the file has 3000 bytes, the program reaches the end of the file when it reads 3000 bytes.

In the C language, when reading a file with getchar() and detecting the end of the file, a special value will be returned, namely EOF (abbreviation for end of file). The scanf() function also returns EOF when end-of-file is detected. Usually EOF is defined in the <stdio.h> file.

#define EOF    (-1)

In the above code, EOF is defined as -1. Why -1? Because the return value of the getchar() function is usually between 0 and 127, these values ​​correspond to the standard character set. However, if the system can recognize the extended character set, the return value of this function may be 0~255. In either case, -1 does not correspond to any character, so this value can be used to mark the end of the file.

How to use EOF, or take the above code as an example:

#include <stdio.h>


int main(void){
    
    
	// ch的类型改为int类型,是因为char只能表示0~255之间的无符号整数,
	//而EOF的值为-1。getchar()函数实际返回的值类型是int,所以它可以读取EOF的值
	int ch;
	// EOF在头文件中已经定义,在这里可以直接使用,不需要再次定义
	// 
	while ((ch = getchar()) != EOF)
	{
    
    
		putchar(ch);
	}
	
	return 0;
}

4. Redirection and files

Programs can use files in two ways. One is to explicitly use specific functions to open files, close files, read files, write files, and so on. The other is to design programs that interact with the keyboard and screen, redirecting input to and output from files through different channels.

A major problem with redirection is that it's OS-dependent, not C-related.


4.1 Redirecting input

Suppose we have compiled the above program and produced an executable file called test.exe.

insert image description here
Open the terminal command line, enter test.exe, press Enter, and run the program.
insert image description here
This is the compiled C language program file executed on the terminal. The result of the operation is the same as that of the compiler.

Suppose you want to program a text file called words. A text file is a file containing text in which the data stored is characters that we can recognize. The content of the file can be an article or a C program. A file that contains machine language instructions, such as a file that stores an executable program, is not a text file.
insert image description here
Since the operation object of this program is characters, a text file is used. Just the command below.

test.exe < words.txt

The output is as follows:
insert image description here
The "<" symbol in the above command is a redirection operator. This operator associates the words file with the stdin stream, uses the contents of the file as input to the test.c program, and prints it on the screen.

4.2 Redirecting output

Suppose you want to use test.c to send the content entered by the keyboard to a file named mywords, you can use the redirection output character to complete the operation.

test.c > mywords

Output result:
insert image description here

Redirect the output character ">", create a new file named mywords, and then redirect the output of test.c to this file. The redirection sends stdout from the display device (monitor) to the mywords file. If the file already exists, the contents of the file will be erased and new contents will be written.

4.3 Combined redirection

Combined redirection is the combination of redirected input and redirected output.

For example: to make a copy of the mywords file, you can use the following command:

test.exe < words > savemywords

insert image description here

It can also be written like this, because the order of commands is independent of redirection.

test.exe > savemywords < words

Note that the file name of the copy cannot be the same as the original file name.

4.4 Attention

1. The redirection operator connects an executable program (including standard operating system instructions) and a data file, and cannot be used to connect a data file to another data file, nor can it be used to connect a program to another program.
2. Using the redirection operator cannot read input from multiple files, nor can it direct output to multiple files
3. Usually, the space between the file name and the operator is not necessary, except occasionally in UNIX Shell, Linux Shell or Windoms Characters with special meaning used in command line prompt mode.

Guess you like

Origin blog.csdn.net/qq_46292926/article/details/127568938