C Language File operations

Source: Internet
Author: User

1, the basic concept of the document

Files: Storing datasets on external media, with a name for the dataset is the file name

Classification of files:

1) User angle: Ordinary files and equipment files

2) What is stored:

ASCII file (text file):

Stored process: Find its corresponding ASCII value according to the text----> to binary---write to file

Read flow: Binary-->10-----to find the corresponding characters--show them

Binary files:

Access to binary data

Process of file operation:

1) Import header file stdio.h

2) define the file pointer

3) Open File

4) Operation file

5) Close the file

2. File pointers

Format: FILE *FP; struct-Body pointer

Function: Stores the first address of a file, pointing to a file

3. Opening and closing of files

Open fopen (file name, operation mode);

fopen ("A.txt", "R")//a.txt default go to products directory to

To close a file:

fclose (file pointer);

4. Read and write data blocks

Write format:

fwrite (variable address, size of data block, number of blocks, FP);

Fwrite (str,sizeof (CH), 1,FP)

Writes the contents of the file pointed to by the STR to the file pointed to by the FP, writes sizeof (CH) byte at a time, writes 1 times

Read format:

Fread (Address of variable, size of data block, number of blocks, FP)

Fread (str,sizeof (CH), 1 fp)

Reads the contents of the file pointed to by the FP into STR, reading sizeof (CH) bytes at a time

5. File read mode

R opens the file as read-only, and the file must exist.

r+ Open the file as read-write, the file must be present.

rb+ Read and write open a binary file, allowing only read and write data.

rt+ Read and write open a text file that allows reading and writing.

W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.

w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.

A write-only file opens in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (EOF character reserved)

A + opens readable and writable files in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (the original EOF character is not preserved)

WB only writes Open or create a new binary file; Only write data is allowed.

wb+ read-write open or create a binary file that allows reading and writing.

wt+ read-write open or create a text file;

at+ Read and write open a text file that allows you to read or append data at the end of the text.

ab+ Read and write open a binary file that allows you to read or append data at the end of the file.

C Language File operations

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.