After writing the "C" single-linked list/two-way linked list of the establishment/traversal/insertion/deletion, how to save the in-memory linked list information in a timely manner to the file, but also timely read from the file for processing, it is necessary to use the "file" of relevant knowledge points for file input and output.
In fact, even if you do not understand the list, but also can learn the "file" relevant knowledge points, but before this is better to have a "pointer" basis.
This article is compiled from the "C Language Programming Tutorial-people post and Telecommunications publishing house," the 12th chapter-document, for the document discussion.
I. Data flow and document concepts
Second, the opening and closing of the file
Iii. sequential Reading and writing of documents
Iv. random reading and writing of files
I. Data flow and document concepts
1. Data flow
Data input and output must pass through the computer peripherals, different peripherals for data input and output formats and methods have different processing, which increases the difficulty of writing file access programs, and it is easy to produce peripherals incompatible with each other problem. Data stream is used to solve this problem.
The data stream considers the data in the entire file as a string of contiguous characters (bytes) without a documented limit.
The data stream accesses the data with the move of the file pointer, where the file pointer currently refers to the data to be processed, and the file pointer is automatically moved backwards after access.
Each data file is followed by a file end sign (EOF), which tells the data file to end, and if the file pointer refers to EOF, the data has been accessed.
2. Documents
"File" means a collection of data stored on an external storage medium (which can be a disk, a disc, a tape, and so on). The operating system manages the data on the external media in the form of a file. When a file is opened or a new file is created, a data stream is associated with an external file (possibly a physical device).
The C language supports streaming files, the previously mentioned data streams, which treat files as a sequence of bytes, accessed in bytes, with no record bounds, that is, the start and end of data input and output are controlled only by the program, not by physical symbols (such as carriage return line breaks).
Files can be categorized from different angles
(1) According to the nature of the file attachment-ordinary documents and equipment files.
(2) According to the organization of the file-sequential read and write files and random read and write files.
(3)--ascii code files and binaries according to the storage format of the files.
The main differences between ASCII and binary files are:
(1) Storage: An ASCII file converts the data type to be stored in a form that can be displayed on the screen, and the binary file is stored in memory as the data type.
(2) Storage space: ASCII occupies more space, and the amount of space is related to the numerical size.
(3) Read and write time: binary files need to be converted to read and write, resulting in slower access speed. ASCII code files are not required.
(4) Function: ASCII file is usually used to store the input data and the final result of the program. Binary files are not displayed for intermediate results in the staging program.
In the C language, standard input devices and standard output devices are processed as ASCII files, which are referred to as standard input files and standard output files, respectively.
Interactive processing of files and memory
Second, the opening and closing of the file
To open the specified file you can use the fopen () function
FILE *fopen (char *filename,char *mode);
The second parameter, mode, is used to set the type of file to open and the access mode for the specified file.
Access mode in the file:
Determine if the file is open correctly
File *fp;fp=fopen (" Files directory "," access mode in file "); if (fp==NULL) { printf ("\ n cannot open the file! "); Getch (); Exit (1);}
Close files using fclose ()
int fclose (FILE *fp);
A return value of 0 indicates a successful shutdown, and if a non-0 value is returned, an error occurs.
A standard device file that is open by the system shuts itself down.
To get the properties of a file:
Get file description Word: int Fileno (file *fp);
Get the corresponding file size according to the file description Word: Long filelength (int handle_no);
Iii. sequential Reading and writing of documents
Sequential read and write refers to the reading or writing of files from beginning to end.
Single character read and write functions: Fgetc () and FPUTC ():
int fgetc (FILE *FP);
Function: Read the file pointer FP currently referred to in the file location of the characters, read, the file pointer automatically moves down one character position, if the file pointer has reached the end of the file, return-1.
Return value: Success returns the character read, and the failure returns-1.
In the FGETC () function call, the read file must be opened in read or read-write mode.
int FPUTC (char ch,file *FP);
Function: Writes the character ch to the location of the file pointed to by the file pointer FP.
Return value: The ASCII code that returns the character on success, and EOF on failure (in Stdio.h, the value of the symbol constant EOF is equal to-1).
The characters that are written can be opened by writing, reading, and appending.
string read and Write functions: Fgets () and fputs ():
Char *fgets (char *str,int n,file *FP);
The function is: reads n characters in the file position referred to by the file pointer FP and puts it into the str character array.
Return value: null is returned if the string is not read.
int fputs (char *str,file *FP);
Function: Writes the string str to the location of the file referred to by the file pointer FP.
Return value: A non-0 value is returned when writing data succeeds, and EOF is returned if the write fails.
Formatted string read-write function: fscanf () and fprintf ():
int fscanf (FILE *fp," formatted string ", "entry Address Table");
The function is to send the data in the file to the input Address table in the format specified by the format string from the file pointed to by the file pointer FP.
Return value: The read data successfully returns the number of data read, and the data in the specified format into an in-memory variable or array, the file pointer moves down automatically. Read failed to return EOF.
int fprintf (FILE *fp," formatted string ", "entry Address Table");
The function is to output the value of the variable in the output Item table to the file location pointed to by the file pointer fp in the format of the format string.
Return value: The number of output characters successfully returned and a negative value returned when failed.
Data block Read and write functions: Fread () and fwrite:
int fread (void *buffer,int size,int count,file *FP);
The function is: starting at the current position of the file pointed to by the file pointer fp, reading the size byte at a time, repeating count times, and saving the read data to the memory area where buffer begins, and moving the read-write position pointer back size*count times.
Return value: The return value of the function is the actual read count value.
int fwrite (void *buffer,int size,int count,file *FP);
The function is: Start with the memory area pointed to by buffer, output size bytes at a time, repeat count times, and put the output data into the file pointed by the FP, and move the read-write position pointer back size*count times.
Return value: Returns the number of data items actually written to count.
Iv. random reading and writing of files
The concept of "c language" file and read-write function of simple data stream