The basic use of c language manipulation text

  • 2020-04-02 02:17:35
  • OfStack

Character read-write function   : the fgetc and fputc
String read-write functions: fgets and fputs
Block read-write functions: freed and fwrite
Format read-write functions: fscanf and fprinf

1. Character reading and writing:
The function of fgetc is to read a character from the specified file.
Character variable =fgetc(file pointer);
Fputc function is to write a character to the specified file, the form of function call is:
Fputc (number of characters, file pointer);

2. Read and write strings
Read the string function fgets
The function reads a string from the specified file into an array of characters.
  Fgets (character array name,n, file pointer);
Write the string function fputs
The fputs function writes a string to the specified file and is called as:
  Fputs (string, file pointer);

3. Read and write data blocks
The general form of a read block function call is:
Fread (buffer, the size, the count, fp);
The general form of write data block function call is:
Fwrite (buffer, the size, the count, fp);

4. Format read and write
The fscanf function, fprintf function is similar to the scanf and printf functions previously used, both of which are formatted read-write functions. The difference between the two is that the read and write objects of the fscanf and fprintf functions are not keyboard and monitor, but disk files.
The call format of these two functions is:
Fscanf (file pointer, format string, input table column);
Fprintf (file pointer, format string, output table column);

File location

There are two main functions to move the internal position pointer of a file, namely the rewind function and the fseek function.

The rewind function, which has been used several times before, is called as:
Rewind (file pointer);
It moves the internal position pointer to the top of the file.

The following is mainly about the fseek function. Fseek function is used to move the internal position pointer of the file. Its call form is:
Fseek (file pointer, displacement, starting point);
Among them:
File pointer points to the file being moved.
The "displacement amount" represents the number of bytes moved, and the displacement amount is required to be long, so that there are no errors when the file length is greater than 64KB. When the displacement is expressed in a constant, the suffix "L" is required.
The "starting point" indicates where the displacement is to be calculated. There are three specified starting points: the beginning of the file, the current position and the end of the file. Its representation is shown in the following table.
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/20140429094330.png? 201432994449 ">

The starting point   The symbol   The figures show that the
File first   SEEK_SET   0
The current position   SEEK_CUR   1
The end of the file   SEEK_END   2

Such as:
The fseek (fp, 100 l, 0);
The meaning is to move the position pointer to the first 100 bytes from the file.

Also note that the fseek function is commonly used for binary files. In text files, there is often an error in the location of the calculation due to the translation.

There are several common file detection functions in c language.
End of file detection function feof function

Call format:
Feof (file pointer);
Function: determine whether the file is at the end of the file. If the file ends, the return value is 1, otherwise it is 0.

Read and write file error detection function
Format of ferror function call:
Ferror (file pointer);
Function: check if the file is read or written with various I/o functions. If ferror returns a value of 0, it means there is no error, otherwise it means there is an error.
File error flag and file end flag set 0 function

Clearerr function call format:
Clearerr (file pointer);
Function: this function is used to clear the error flag and the end of the file, making them zero.


Related articles: