(C language) Basic knowledge of file operations

Foreword

Now that you have come into contact with programming languages, friction with files is inevitable. This blog will learn about file operations.

1. Opening and destroying files

Opening a file is the most basic in the use of files. It is usually FILE* pf = fopen(“file name”, “file opening method”). There are many ways to open files:

How to use it If the file does not exist

“r” (read only) Error opening an existing text file for data entry.

“w” (write only) To output data, open a text file Create a new file

“a” (append) Add data to a text file Create a new file

“rb” (read only) Error opening a binary file for input data

“wb” (write only) To output data, open a binary file Create a new file

“ab” (append) Add data to the end of a binary file Error

“r + ” (read and write) Open a text file for reading and writing Error

“w + ” (read and write) Create a new text file for reading and writing Create a new file

“a + ” (read and write) Open a text file, read and write at the end of the file, create a file

“rb + ” (read and write) Open a binary file for reading and writing Error

“wb + ” (read and write) Create a new binary file for reading and writing Create a new file

“ab + ” (read and write) Open a binary file, read and write at the end of the file, create a new file

There are two ways to fill in the file name:

The first absolute address: detailed to the house number, such as “C:\Users\86199\source\repos”

The second type of relative address: I only write the file name and its suffix. For example, I simply create a text file “test.txt”

The function used to destroy files is called fclose. Its function is to destroy and release the created file pointer variable. This step is essential, just like if you borrow a pen from someone else and you still have to return it after using it, the same principle , the usage method is fclose (file pointer variable).

2.fputc/fgetc/fputs/fgets

fgetc() Character input function All input streams
fputc() Character output function All output streams
fgets() Text line input function All input streams
fputs() Text line output function All output streams

Maybe you don’t understand what input stream and output stream are. Let me give you an analogy:

So fputc means writing from memory to the hard disk, and fgetc means writing from memory to the hard disk.

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

int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
}
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fputc('d', pf);

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

This code is that I created a text file named data.txt and wrote 4 characters in fputc.

int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
}
\t
int ch = fgetc(pf);
printf("%c ", ch);
ch = fgetc(pf);
printf("%c ", ch);
ch = fgetc(pf);
printf("%c ", ch);
ch = fgetc(pf);
printf("%c ", ch);
fclose(pf);
pf = NULL;
return 0;
}

I won’t go into too much detail below. It’s basically what I said before. Let’s go directly to the code:

fputs:

int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
}

fputs("Hello\\
", pf);
fputs("World", pf);

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

fgets:

int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
}
char c[20] = { 0 };
fgets(c, 5, pf);

printf("%s", c);

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

in:

char*_Buffer is an array of char type;

int_MaxCount is the number you want to read;

FILE*_Stream is a pointer to the file you created.

But you see above, I want to read 5 char characters, but it only reads 4. What is the problem? Please see:

The number of characters it reads is int_MaxCount-1, but what will happen if the number we read exceeds the number of characters we have?

It can be seen that it exceeds the entire length, it will not continue reading, it will add ‘\0’ at the end.

3.fprint/fscanf

fscanf() Format input function All input streams
fprintf() Format output function All Output stream
struct A
{
int s1;
char s2;
int s3;
};
int main()
{
struct A a = { 100,'w',100 };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
}
\t
fprintf(pf, "%d-%c-%d", a.s1, a.s2, a.s3);

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

It can be seen that there is a big difference between fprintf and the printf we usually use. We all know that printf is an output function, but why is it not output on the screen, but on text? It can also be said that the memory is written on the hard disk. data. fscanf and scanf also have opposite effects, for example:

struct A
{
int s1;
char s2;
int s3;
};
int main()
{
struct A a = { 0 };
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
}

fscanf(pf, "%d-%c-%d", & amp;(a.s1), & amp;(a.s2), & amp;(a.s3));
printf("%d-%c-%d", a.s1, a.s2, a.s3);
fclose(pf);
pf = NULL;
return 0;
}

4.fwrite/fread

fread() Binary input File
fwrite() Binary output File

fwrite:

int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
FILE* pf = fopen("data.txt", "wb");
if (pf == NULL)
{
perror("fopen");
}

fwrite(arr, sizeof(arr[0]), sizeof(arr) / sizeof(arr[0]), pf);
fclose(pf);
pf = NULL;
return 0;
}

As for why the text cannot be recognized, it is because the machine only understands binary.

fread:

int main()
{
int arr[10] = { 0 };
FILE* pf = fopen("data.txt", "rb");
if (pf == NULL)
{
perror("fopen");
}

fread(arr, sizeof(arr[0]), sizeof(arr) / sizeof(arr[0]), pf);
int i = 0;
for (i = 0; i < 10; i + + )
{
printf("%d ", arr[i]);
}
fclose(pf);
pf = NULL;
return 0;
}

End

Then our file comes to an end. Maybe you don’t have a deep understanding of the input and output streams of files, but as long as you know that writing memory to the hard disk is output, reading from the hard disk is input. Bye!