C language-summary of common related operation knowledge points of files

table of Contents

 

1. File name

Second, the file path

2.1 Absolute path

2.2 Relative path

2.3 Matters needing attention

3. Why use files

Fourth, the classification of documents

Five, how to operate the file in the code

5.1 Open file fopen

5.2 Read and write files

5.3 Close the file fclose

Six, the end of the file judgment

Seven, file buffer

Eight, supplement


1. File name

On the computer, each file has its own file name. The shape is like: "sky.jpeg". Generally, the file name is divided into two parts: ①sky name②.jpeg extension

The use of the extension: ① to distinguish the types of files ② which program is used by the high-speed operating system to open the file by default, more accurately, the "path" of the file is used as the identification of the file on the system.

Second, the file path

2.1 Absolute path

Form: D:\program\ha\haha.exe

2.2 Relative path

Need to give a "reference system" to tell us from which directory to find this file

If D:\program\ha is used as the reference system, then .\haha.exe is the relative path

2.3 Matters needing attention

\ Is called a backslash / is called a slash 

D:/program/ha/haha.exe is also possible. On Linux/Mac, only / is supported as a separator.

It is more recommended to use /.

\ Is easy to combine with other characters to escape, so it is not recommended, such as printf("D:\program\test");

3. Why use files

Because of "persistence", persistence is to keep data stored continuously, and writing to disk/file is a simple but most commonly used solution. (The data on the disk will also be lost, usually several years-decades)

Fourth, the classification of documents

From a programming point of view, files can be divided into two categories:

①Text file 

②Binary file

For example, to store an integer 10000, you can save it on disk

Text file: '1','0','0','0','0'

Binary file: 10 27 00 00

How to distinguish: If you open a file with Notepad and find that you can understand it, it is a text file, otherwise, it is a binary file. (Because Notepad is opened according to the "text file" method)

Five, how to operate the file in the code

5.1 Open file fopen

The first parameter is the file name, and the second parameter indicates the opening method. The included header file is: #include<stdio.h>.

Understanding of the second parameter:

1) Operate on "text files"

   r : read, read-only; in order to input data, open an existing text file.

   w : write, write only; in order to output data, open a text file, if the text file does not exist, create the file.

   a : append, append writing; add data to the end of the text file

   r+ : read and write; for reading and writing, open a text file

  w+ : read and write; in order to read and write, suggest a new file

  a+ : read and write; open a file, read and write at the end of the file

2) For binary file operations

   rb : read-only; in order to input data, open a binary file

   wb : write only; in order to output data, open a binary file, if the binary file does not exist, create the file.

   ab : append; add data to the end of a binary file

   rb +: read and write; open a binary file for reading and writing

   wb +: read and write; in order to read and write, create a new binary file

   ab+ : read and write; open a binary file, read and write at the end of the file

3) Attention

①If the specified file "w", "wb", "w+", "a+", "wb+", "ab+" does not exist, a new file will be created, and other errors will occur;

②Opening the file corresponds to closing the file. I will introduce the closing of the file below.

5.2 Read and write files

5.2.1 Sequential reading and writing of files:

fgetc : character input function; applicable to all input streams;

fputc : character output function; applicable to all output streams

fgets : text line input function; applicable to all input streams;

fputs : text line output function; applicable to all output streams

fscanf : formatted input function; applicable to all input streams

fprintf : formatted output function; applicable to all output streams

fread : Binary input; suitable for files

fwrite : binary output; suitable for files

E.g:

#include<stdio.h>
#include<stdlib.h>

int main()
{
	FILE* pfile;
	pfile = fopen("haha.txt", "w");
	if (pfile != NULL){
		fputs("fopen example", pfile);
		fclose(pfile);
	}
	return 0;
}

Since haha.txt does not exist, but the parameter of fopen is "w", the file will be created, and "fopen example" will be written to the file through fputs. When fopen opens the file, we need to close the file corresponding to fclose , and finally we can see the computer There is an extra file named haha.txt on it, after opening it as shown in the figure below:

5.2.2 Random read and write of files

①fseek: can be understood as modifying the position of the "cursor" (cursor)

The first parameter stream is the file pointer ;

The second parameter offset is the offset , a positive number indicates a positive offset, and a negative number indicates a negative offset;

The third parameter origin sets the offset from the file , and may take values: SEEK_SET (the beginning of the file), SEEK_CUR (the current position) or SEEK_END (the end of the file);

 

E.g:

#include<stdio.h>

int main()
{
	FILE* pfile;
	pfile = fopen("example.txt", "wb");
	fputs("This is an apple.", pfile);
	fseek(pfile, 9, SEEK_SET);
	fputs("sam", pfile);
	fclose(pfile);
	return 0;
}

②ftell : Returns the offset of the file pointer relative to the starting position

③rewind: Let the position of the file pointer return to the starting position of the file

5.3 Close the file fclose

As mentioned above, the function to close the file is fclose, and its prototype is as follows

Six, the end of the file judgment

In the process of file reading, the return value of the feof function cannot be used directly to determine whether the file is over, but when the file reading is over, it is judged whether the reading fails or the end of the file is encountered.

6.1 Whether the text file reading is over, judge whether the return value is EOF (fgetc), or NULL (fgets)

fgetc judges whether it is EOF, and returns -1 EOF EOF end of file

fgets determines whether the return value is NULL, and returns NULL to read.

6.2 Judgment of the end of binary file reading, and judge whether the return value is less than the actual number to be read

The judgment that the file has been read depends on which function is being used for reading.

If you are using fread, the return value indicates the number of elements actually read n, and the third parameter indicates the number of elements expected to be read count. If n<count, it is considered finished. If the file is not finished, n==count must be equal.

The understanding is as follows: Suppose the file has 110 elements, and each element is a char (1 byte). When fread reads, count is set to 20.

The first time: n=>20, 20 bytes are read

Second time: n=>20, read [20,40) bytes

The third time: n=>20, read [40,60) bytes

Fourth time: n=>20, read [60,80) bytes

Fifth time: n=>20, read [80,100) bytes

Sixth time: n=>10, read [100,110) bytes

Seven, file buffer

The purpose is to improve the efficiency of reading and writing files

Core principle: The efficiency of accessing memory is much higher than that of accessing external memory.

Eight, supplement

1) The so-called "print to console" is more rigorously called, writing data to "standard output", which is a special file in the operating system. An important idea in the operating system: "Everything is a file." The operating system needs to manage a lot of software resources and hardware devices. In order to facilitate management, the operating system abstracts these things into "files".

2) In the C standard library and some system functions, many functions will set the error information to the errno error code when they execute errors, and then the cause of the error can be identified through this error code. If the program is correct, errno is 0 by default at this time. If it is not 0, it means that the last operation went wrong.

strerror : Convert the error code (errno) into the corresponding text explanation

3) When a program is started, three files are opened by default (standard input stdin, standard output stdout, standard error stderr)

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/110346727