C Key summary of the document

Source: Internet
Author: User

C File Key Knowledge summary

Program Files
Data Files---> sub-text Files (ASCII files) and image files (binary files). The distinction is whether you can read it when you open it with Notepad.

It takes less time to read and write with binary files, because a text file requires a process of conversion,

file buffers, input file buffers---> Disk files when the data is read into memory, it needs to be saved to the buffer before it is loaded and sent to memory.
The output file buffer---> Memory data is written to the disk file as above.

File Information area: Each used file, in memory automatically opens up a file information area, used to save the file information. This information is present in the structure variable (System auto-generated),
Named file, with the files *FP; Defines a pointer variable that points to the file information area for use with the file. That is, the pointer allows you to find the file associated with it.
It should be noted that this pointer does not point to the beginning of the data file on the external media, but to the beginning of the file information area.

File functions:
First, open and close the file:
1.fopen (filename, using file mode);
----> successfully returns the first address of the file information area, the failure returns null;
The file name can be a string constant, enclosed in double apostrophes. such as "File1.dat". You can specify a path, because \ is the leading of the escape character in the C language, and the path is indicated by \ \.
For example, "F:\\aa\\file1.dat" means to open the File1 file under the AA folder of F Drive. If not specified, the file is generally automatically placed in the same folder as the source program file (depending on the compilation system).

There are 6 basic ways to use files.
"R": Read-only. Open the file in order to read the data in the text file. If the file does not exist, a null value is returned. The file location tag is at the beginning of the file.
"W": Write only. In order to write data to a text file to save. If the file does not exist, an attempt is made to automatically create a file that you have named. Will generally try to succeed. The file location tag is at the beginning of the file. It should be noted that if you want to write and open a file that already exists, the system will delete the file and re-establish a file with the same name, the data in the source file will be lost.
"A": Append. Append data to the end of the text file, writable, and the original data does not disappear. The file location is marked at the end of the file data.
"RB", "WB", "AB" correspond to binary files respectively.

2.fclose (file name)

Because the data exists in the buffer first. So the program ends, you don't close the file, and the information in the buffer is lost. (Although this is not the case for most compiled systems, it should be avoided). Thus, before the end of the program we have to close the file and force the data in the buffer to be persisted to the file.

Second, sequential read and write data files:
1. Read and write characters
1) fgetc (FP); Read in memory one character from the file that the FP points to. The character was successfully returned, and the end of the file failed to return the symbol EOF (-1);
2) FPUTC (CH,FP): Writes the character ch to the file FP. The character returned successfully, and the failure returns EOF.

2. Read and Write strings
1) fgets (STR,N,FP); reads a string with a length of n-1 from the FP (because you want to leave one), and it is stored in the character array str. The STR address was successfully returned, and the failure returned null;
2) fputs (STR,FP); writes the character array str to the FP. A successful return of 0 fails to return a value other than 0.
It should be noted that when writing a string to a file, each time a string is written, the barracks writes a \ n to differentiate the individual strings.

3. Read and write a set of data to a binary file, or a piece of data:
1, Fread (BUFFER,SIZE,COUNT,FP) reads from the fp count of size bytes of data, stored in an array, the address of this array is buffer. Read successfully returns the Count value.
2, Fwrite (BUFFER,SIZE,COUNT,FP) The buffer address points to the array of data, take count of size bytes, saved in the FP. The count is returned successfully.
The general form of writing is this:
if (fwrite (&stu[i],sizeof (struct Student), 1,FP)!=1)
{
printf ("File Wrute error!\n");
Exit (0);
}
feof (FP); Check the last read and write status, default is 0, read and write successful return 1,else return 0.



C Key summary of the document

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.