C language file read/write notes

Source: Internet
Author: User
Tags fread rewind

1. File opening function: fopen

Call method: file pointer name = fopen (File Name (PATH), file method)

Eg: file * FP;

Fp = fopen ("file1.bat", "rb ");

File Usage

"RT" only opens a text file and only supports reading data.

"WT" can only open or create a text file and only allow Data Writing

"At" append a text file and only allow data writing at the end of the file

"Rb" open a binary file in read-only mode and only allow reading data

"WB" only writes to open or create a binary file, only data can be written

Append a binary file and write data at the end of the file.

"RT +" read/write to open a text file, allowing read and write

"WT +" read/write to open or create a text file, allowing read/write

"At +" read/write mode to open a text file, allow reading, or append data to the end of the file

"RB +" read/write to open a binary file and allow read/write

"WB +" read/write or create a binary file, allowing read/write

"AB +" read/write to open a binary file, allow reading, or append data to the end of the file

Open the file in "A" mode. However, the file must exist at this time; otherwise, an error occurs.

2. fclose Function

Common call format: fclose (File pointer)

Eg: fclose (FP): When the file close operation is completed normally, the return value of the fclose function is 0; otherwise, an error occurs.

3. Some read/write functions:

Character read/write functions: fgetc and fputc

String read/write functions: fgets and fputs

Data Block read/write functions: fread and fwrite

Formatted read/write functions: fscanf and fprinf

Eg: fgetc function call form:

Ch = fgetc (FP); read one character from open file FP and send it to Ch

Fputc function call format:

Ch = fputc ('D', FP); write the character D to the file pointed by FP

Rewind function: used to move the internal position pointer of the FP object to the file header: rewind (File pointer)

Fp = stdout: output to the standard output file stdout, and display the file content on the display;

Usage: fp = stdout;

Fputc (CH, FP );

Fgets: reads a string from a specified file to a character array.

Usage: fgets (character array name, N, file pointer) n indicates that the read string cannot exceed n-1 characters, and the '\ 0' sign is added to the end of the last character.

Note: If a linefeed or EOF is received before n-1 characters, the read ends. The return value of the fgets function is the first address of its character array.

Fputs: writes a string to a specified file.

Usage: fputs (string, file pointer): fput ("ABCD", FP)

Fread \ fwrite: read/write of data blocks:

Usage: fread (buffer, size, Count, FP)

Fwrite (buffer, size, cout, FP); buffer is a pointer, which indicates the first address of the input data in the fread function. In fwrite

The first address for storing output data; size indicates the number of bytes of data; count indicates the data block read/write; FP file pointer

Fread (BUF, FP); read 4 bytes (INT) from the file FP and send it to the Buf for 5 consecutive reads (and read 5 Int)

The fscanf \ fprintf function is used with scanf and printf
Functions have similar functions. They are all formatted read/write functions.

The difference between the fscanf function and fprintf function is that the Read and Write objects are not keyboard and display, but disk files.

The call formats of these two functions are:

Fscanf (File pointer, Format String, input table column );

Fprintf (File pointer, Format String, output table column );

Eg:
Fscanf (FP, "% d % s", & I, S );
Fprintf (FP, "% d % C", J, CH );

4. Random File Reading

Rwind;

Fseek function: it must be noted that the fseek function is generally used for binary files. In a text file, the location of the calculation is often incorrect because the conversion is required.

The fseek function is used to move the internal position pointer of a file. The call form is fseek (File pointer, displacement, starting point). The "file Pointer" points to the object to be moved.

"Displacement" indicates the number of bytes to move. The displacement is long data, so that no error occurs when the file length is greater than 64 KB. When a constant is used to represent the displacement, the suffix "L" is required ".

The "Starting Point" indicates where to start the calculation of the displacement. There are three defined starting points: the first part of the file, the current position, and the end of the file.
The representation is as follows:
Starting point represents a symbolic number.
── ─
File head SEEK-SET0
Current location SEEK-CUR1
Seek-end at the end of the file
2
For example:
Fseek (FP, 100l, 0); it means to move the position pointer to the first 100 bytes away from the file. It must be noted that the fseek function is generally used for binary files. To convert a text file,

Therefore, errors often occur in the calculated position. After moving the pointer, you can use any of the read/write functions described earlier.

Since it is generally used to read and write a data block, fread and fwrite functions are often used.

5. File Check

Feof: file end Detection: feof (File pointer): determines whether it is in the end of the file (1 or 0 is returned)

Ferror: read/write error detection, checking whether file read/write errors (1 or 0 is returned)

Clearerr: file error mark and file end sign 0 function. Clear the error mark and file end sign so that they are 0 values.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.