Concept of the "C Language" file and the read/write functions of simple data streams, and the concept of data streams
After writing the "c" single-chain table/two-way linked list creation/traversal/insertion/deletion, How can I save the linked list information in the memory to the file in a timely manner, in addition, you can read and process files in a timely manner, and you need to use the "file" related knowledge points for file input and output.
In fact, even if you do not know the linked list, you can also learn the "file" related knowledge points, but it is best to have a "Pointer" basis before that.
This article is based on the C language programming tutorial-people's post and telecommunications press chapter 12th-document for discussion.
1. Concepts of data streams and files
Ii. Opening and Closing of Files
Iii. sequential file read/write
4. Random file read/write
1. Concepts of data streams and files
1. Data Stream
Data input and output must be processed by computer peripherals. Different peripheral devices have different processing methods for data input and output formats and methods, this increases the difficulty of writing file access programs, and easily causes incompatibility between peripheral devices. Data Stream is used to solve this problem.
The data stream regards the data in the entire file as a string of consecutive characters (bytes) without the record restrictions.
The data stream uses the movement of the file pointer to access the data. The current position of the file pointer is the data to be processed. After access, the file pointer will automatically move backward.
Each data file is followed by an end symbol (EOF) to indicate the end Of the data file. If the file Pointer Points to EOF, the data has been accessed.
2. File
Files are data sets stored on external storage media (such as disks, CDs, and tapes. The operating system manages data on external media in the form of files. 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, that is, the data stream mentioned above. It regards files as a byte sequence and accesses the files in bytes without record boundaries, that is, the start and end of data input and output are only controlled by the program, not by physical symbols (such as carriage return or line break.
Files can be classified from different angles.
(1) According to the nature of file attachment-common files and device files.
(2) read and write files and random read/write files in sequence according to the file organization form.
(3) Based on the storage format of files-ASCII and binary files.
The main differences between ASCII and binary files:
(1) Storage Format: ASCII files can be converted to display on the screen for storage. binary files are stored in the memory based on the data type.
(2) storage space: ASCII occupies a large amount of space, and the size of space is related to the value size.
(3) read/write time: binary files need to be converted during read/write, resulting in slow access. ASCII files are not required.
(4) Purpose: ASCII files are usually used to store input data and the final results of the program. Binary files cannot be displayed and are used to store intermediate results of programs.
In C language, standard input devices and standard output devices are processed as ASCII code files, which are called standard input files and standard output files respectively.
Interaction between files and memory:
Ii. Opening and Closing of Files
You can use the fopen () function to open a specified file.
FILE * fopen (char * filename, char * mode );
The second parameter is used to set the file type to be opened and the Access mode of the specified file.
File Access Mode:
Determine whether the file is opened correctly
FILE * fp; fp = fopen ("FILE directory", "FILE Access Mode"); if (fp = NULL) {printf ("\ n cannot open this FILE! "); Getch (); exit (1 );}
Close the file using fclose ()
Int fclose (FILE * fp );
If the return value is 0, the operation is successful. If the return value is not 0, an error occurs.
Standard Device files opened by the system are closed by the system.
Get object attributes:
Get the FILE Description: int fileno (FILE * fp );
Obtain the file size based on the file Description: long filelength (int handle_no );
Iii. sequential file read/write
Sequential read/write refers to reading or writing data from start to end individually.
Single-character read/write functions: fgetc () and fputc ():
int fgetc(FILE *fp);
Function: Read the characters in the position currently referred to by the file pointer fp. After reading is completed, the file pointer is automatically moved down to one character position. If the file pointer has reached the end of the file,-1 is returned.
Return Value: if the operation succeeds, the read characters are returned. If the operation fails, the return value is-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: Write the character ch to the position of the file pointed to by the file pointer fp.
Return Value: returns the ASCII code of the character upon success, and EOF upon failure (in stdio. h, the value of the symbol constant EOF is equal to-1 ).
The written characters can be opened by writing, reading, writing, and appending.
String read/write functions: fgets () and fputs ():
char *fgets(char *str,int n,FILE *fp);
Function: Read n characters at the position indicated by the file pointer fp and put them in the str character array.
Return Value: If the string cannot be read, NULL is returned.
int fputs(char *str,FILE *fp);
Function: Write the string 'str' into the position of the file indicated by the file pointer fp.
Return Value: if the data is successfully written, a non-zero value is returned. If the data fails to be written, an EOF is returned.
Formatted string read/write functions: fscanf () and fprintf ():
Int fscanf (FILE * fp, "formatted string", [input address table ]);
Function: send data in the file to the input address table in the format specified by the format string from the file pointer fp.
Returned value: the number of data read is returned successfully, and the data is saved to a variable or array in the memory in the specified format. The file pointer is automatically moved down. EOF is returned if reading fails.
Int fprintf (FILE * fp, "formatted string", [input address table ]);
Function: output the variable values in the output item table in the format specified by the format string to the file location pointed to by the file pointer fp.
Return Value: the number of output characters is returned successfully. If the return value fails, a negative value is returned.
Data Block read/write functions: fread () and fwrite:
int fread(void *buffer,int size,int count,FILE *fp);
Function: starts from the current position of the file pointed to by the file pointer fp, reads size bytes at a time, repeats count times, and stores the read data in the memory zone starting with buffer, at the same time, the read/write position pointer is moved back by size * count.
Return Value: the return value of this function is the actually read count value.
int fwrite(void *buffer,int size,int count,FILE *fp);
Function: starts from the memory area pointed to by the buffer, outputs size bytes at a time, repeats count times, and puts the output data into the file pointed to by the fp, at the same time, the read/write position pointer is moved back by size * count.
Return Value: return the number of actually written data items count.
4. Random file read/write
Random read/write refers to the internal position pointer of a removable file to the position where the file needs to be read and written before reading and writing. This type of read/write is called random read/write. The key to achieving random file read/write is to move the location pointer as required, which becomes the location of the file.
void rewind(FILE *fp);
Function: Move the position pointer inside the file to the starting position of the file.
int fseek(FILE *fp,long offset,int whence);
Function: moves the file pointer from the whence address to the offset address.
Return Value:
The pointer offset of the corresponding file is not clearly described in the function definition. When the offset is a positive number, it is offset to the end of the file, when it is a negative number, it is offset to the file header. Here, we need to pay attention to the relationship between the offset size and the file size boundary.
When offset is an offset to the end of a file, fseek returns 0 regardless of whether the offset exceeded the end of the file. When the offset does not exceed the end of the file, the file Pointer Points to the normal offset address. When the offset exceeds the end of the file, the file Pointer Points to the end of the file. The offset error-1 value is not returned.
When offset is offset to the file header, if the offset does not exceed the file header, it is normal offset. The file Pointer Points to the correct offset address, and the return value of fseek is 0. when the offset value exceeds the file header, fseek returns the error-1 value, and the file pointer remains unchanged at the original address.
long ftell(FILE *_File);
Function: Obtain the current position of the streaming file, which is expressed by the displacement relative to the beginning of the file. Because the position pointers in files often move, it is often difficult for people to know their current position. Use the fell function to obtain the current position.
Returned value:-1L indicates an error.
int putw(int _Ch,FILE *_File);
Function: Output integer _ Ch to the file pointed to by fp.
int __cdecl getw(FILE *_File);
Function: returns an integer from the disk file to the memory.