[C language] File operation "1"

  Personal homepage: Welcome everyone --> Populus euphratica under the desert

 Guys, you are so beautiful

 If you find the article helpful

 You can support bloggers with one click

 Every ounce of your concern is the driving force for me to persevere

 

 ☄: Focus of this issue: Knowledge about file operations

  I hope you all have a happy study and work every day. 

What is a file? Why have documents? What is the filename?

The files on the disk are files, but in programming, we generally talk about two kinds of files: program files and data files (classified from the perspective of file functions).
program files:
Including source program files (suffixed with .c ) , object files ( suffixed with .obj in windows environment ) , executable programs ( suffixed with .exe in windows environment ) .
data file:
The content of the file is not necessarily the program, but the data read and written when the program runs, such as the file from which the program needs to read data, or the file from which the content is output.
This is called a file.
Our general data persistence methods include storing data in disk files, storing data in databases, and so on. Using files, we can store data directly on the hard disk of the computer, which makes the data persistent, which is why we use files.
A file must have a unique file identifier for user identification and reference. The file identifier consists of three parts: file path + file name trunk + file suffix, generally we also call the file identifier the file name.

 The concept of flow:

A stream is a channel formed between a system and a program. The two are a one-to-one relationship, and the flow expresses the ability to interact between the two. The so-called "ANSI C program runtime system must provide three streams, stdin, stdout, stderr", that is, the system must provide these three "channels" , namely

1, the channel stdin between the program and the standard input (default keyboard)

2, the channel stdout between the program and the standard output (default display)

3, the channel stderr between the program and the terminal (the default is also the display)

There is also the file stream that we want to focus on today, using the FILE* pointer to point to the memory for subsequent operations.

file opening and closing

The file should be opened before reading and writing, and should be closed after the end of use . When writing a program, when opening a file, it will return a FILE* pointer variable to point to the file, which is also equivalent to establishing a pointer and file relationship. ANSIC stipulates that the fopen function is used to open the file and fclose to close the file.

  The front of fopen is the file name, and some specific strings are required after it, and fclose only needs to close the file pointer variable.

file read and write operations

We just know how to open and close files is not enough, we should also know how to read and write files.

The above is the commonly used file operation function

Here's what we actually do:

Write the numbers 0~9 to the file:

int main()
{
	FILE *p = fopen("胡杨.dat", "w");
	if (NULL == p)
	{
		perror("main:");
		exit(-1);
	}

	for (char i = '0'; i <= '9'; i++)
	{
		fputc(i,p);
	}

	fclose(p);
	p = NULL;
	return 0;
}

 

The write-only form "w" is used to open the file, the fputs function is used to write characters, and then a loop is used to insert each character from 0 to 9 into the file.

 Read the contents of the file

int main()
{
	FILE *p = fopen("胡杨.dat", "r");
	if (NULL == p)
	{
		perror("main:");
		exit(-1);
	}

	char a[15] = { 0 };

	fgets(a, 11, p);

	puts(a);

	fclose(p);
	p = NULL;
	return 0;
}

 Here's a little question, why is 11 used in the fgets function? There are 10 numbers in total, 11 times to read?

Because the default copy of '\0' is to copy the end of the string, which can be seen through debugging.

 It can be seen that if 10 copies are copied, then only 8 will be copied and stopped, that is, the last one will copy '\0' by default

So when using fgets, there is one more element than the actual copy, that is, if you want to copy 10 elements, then 11 should be written in fgets.

read and write in binary

We want to write some confidential file in file, can write in binary in file.

Let's take the structure as an example for the convenience of observation:

typedef struct stu
{
	char name[20];
	int age;
	int ID[10];
}stu;

int main()
{
	FILE *p = fopen("树下.dat", "wb");
	if (NULL == p)
	{
		perror("main:");
		exit(-1);
	}
	
	stu s1 = { "胡杨树下", 18, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } };

	fwrite(&s1, sizeof(s1), 1, p);
	

	fclose(p);
	return 0;
}

 Binary writes, we don't understand, but files know .

Let's open with binary .

int main()
{
	FILE *p = fopen("树下.dat", "rb");
	if (NULL == p)
	{
		perror("main:");
		exit(-1);
	}

	stu s2 = { 0 };

	fread(&s2, sizeof(s2), 1, p);

	fclose(p);
	return 0;
}

 This can be passed, the binary is read, and the file is remembered.

Precautions

1. First of all, when we need to know a file to operate, we must first consider the opening method , whether it is read-only or write-only or other methods.

2. Determine whether the file is opened successfully .

3. To judge how to write to the file, whether to input one by one, line by line, or output, the functions used are different.

4. At the end you want to close the file .

Next notice:

The next issue will explain the knowledge of the file series.

Wonderful next issue~! ~! ~!

 

 

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124341622