Once a stream is enabled, you can select among three different types of unformatted I/O to read and write the stream:
(1) I/O of each character. One read or write character at a time. If the stream is buffered, standard I/O processes all the buffers.
(2) I/O of each row. If you want to read or write a row at a time, use fgets and fputs. Each line ends with a linefeed. When fgets is called, the maximum capacity that can be processed should be stated.
(3) Direct I/O. Fread and fwrite functions support this type of I/O. Each I/O operation reads or writes a certain number of objects, and each object has a specified length. These two functions are often used to read or write a structure from a binary file each time.
The term direct I/O comes from the ISO C standard, it is also called binary I/O, an object I/O at a time, a record-oriented I/O, or a structure-oriented I/O.
1. Input Functions
The following three functions can be used to read one character at a time.
#include <stdio.h> getc( FILE * fgetc( FILE * getchar(
The getchar function is equivalent to getc (stdin ). The difference between the first two functions is that getc can be implemented as a macro, while fgetc cannot be implemented as a macro. This means:
(1) The getc parameter should not be an expression with side effects.
(2) Because fgetc must be a function, you can get its address. This allows the fgetc address to be transmitted to another function as a parameter.
(3) It may take longer to call fgetc, because it usually takes longer to call a function than to call a macro.
When these three functions return the next character, they will convert their unsigned char type to int type. The reason is that if the highest bit is 1, the return value is not negative. The reason that the integer return value is required is that, in this way, all possible characters can be returned plus an error or indicator value that has reached the end of the file. The constant EOF in <stdio. h> is required to be a negative value, and its value is often-1. This means that the return values of these three functions cannot be stored in one character variable. In the future, the return values of these functions will be compared with the constant EOF.
Note that the three functions return the same value whether an error occurs or the end of the file is reached. To distinguish between the two cases, you must call ferror or feof.
#include <stdio.h> ferror( FILE * feof( FILE * clearerr( FILE *fp );
In most implementations, two flags are maintained for each stream in the FILE object:
- Error mark.
- End mark of the file.
Call clearerr to clear the two flags.
After reading data from the stream, you can call ungetc to compress the characters and send them back.
#include <stdio.h> ungetc( c, FILE *
The characters in the push back can be read from the stream again, but the order of the characters is the opposite of that in the push back. It should be noted that although iso c allows the implementation to support sending back at any number of times, it requires the implementation to provide only one character at a time. We cannot expect to return multiple characters at a time.
The send-back character does not have to be the character that was last read. EOF cannot be sent back, but when the end of the file is reached, a character can still be sent back. This character will be returned for the next read and EOF will be returned for the next read. The reason for this is that a successful ungetc call will clear the end mark of the file of the stream .??? (The more explained, the more confused)
When you are reading an input stream and performing some form of word or mark-breaking operations, the return character operation is often used. Sometimes you need to take a look at the next character to determine how to process the current character. Then, you need to conveniently return the character you just viewed so that this character can be returned the next time you call getc. If the standard I/O Library does not provide the delivery capability, we need to store the character in our own variable, set a flag to determine whether to call getc or use it from our own variables when a character is needed next time.
When ungetc is used to send back characters, they are not written to files or devices, but are written back to the stream buffer of the standard I/O library.
2. Output Functions
Each input function described above has an output function.
#include <stdio.h> putc( c, FILE * fputc( c, FILE * putchar(
Like the input function, putchar (c) is equivalent to putc (c, staout). putc can be implemented as a macro, while fputc cannot.