C Standard Library (2) -- # include (stdio. h)
C language standard library (2) -- # include By jxlijunhao is included in this header file, including single-character and string processing functions, standard input operations, stream operations, read from files, Write Functions, block I/O operations, etc. Part of the record below
Standard Input and Output:
Int getchar (void); // read a character from the standard input
Int putchar (int character); // output to standard output
Stream operation:
Int putc (int character, FILE * stream); // write characters to an input stream
Int getc (FILE * stream); // read a character from the output stream
Which is equivalent to the above functions?
int fgetc ( FILE * stream );
int fputc ( int character, FILE * stream );
FILE * fopen (const char * filename, const char * mode); // open the FILE
"R": Read data "w": If the opened file name already exists, the original content will be cleared and the new content will be written again. "r + ": read/update "w +": Write/update. If the file exists, the old content is cleared. "a +": Add new content to the file.
int fclose ( FILE * stream );
The following is a simple example. input data from a standard input and save it to a file.
# Include
Int main () {// read a group of data from the keyboard. If the input contains '#', it ends and is saved to the text FILE char c; FILE * pFile; pFile = fopen ("myFile.txt", "w +"); while (1) {c = getchar (); if (c = '#') break; fputc (c, pFile) ;}fclose (pFile); // read the data in the file and show pFile = fopen ("myFile.txt", "r"); if (pFile! = NULL) {while (c! = EOF) {c = fgetc (pFile); putchar (c) ;}} fclose (pFile );}
Block I/O operations: File Location
Int fseek (FILE * stream, long int offset, int origin); // FILE Location
Start with the third parameter and start offset calculation. There are three optional values: SEEK_SET: Start from the file SEEK_CUR: Start from the file SEEK_END: from the end of the file (note that the offset value must be set to a negative number, starting from-1)
In the following example, replace the 5th position in "this is a tste file" with lowercase letters.
Int main () {FILE * pFile; pFile = fopen ("test.txt", "wb"); fputs ("this is a test file. ", pFile); fseek (pFile, 5, SEEK_SET); // fputs (" I ", pFile); fclose (pFile );}