The previous article of this article:C/C++ file operation (full of details, part1)_There are still unknown blogs waiting to be explored-CSDN Blog
Personal homepage:There are still unknowns waiting to be explored_C language difficulties, data structures, small projects-CSDN blog
Special column:C language difficulties_There are still unknown blogs waiting to be explored-CSDN blog
Table of contents
1. Input stream and output stream
3. Random reading and writing of files
4. Binary files and text files
5. Sign of the end of file reading
2. Binary file reading end flag
I. Introduction
Through the previous article, you have already understoodwhat is a file, and the operations of opening and closing files , The concept of flow, etc.
If you feel a little unfamiliar with the above content, you might as well take a look. The article link is at the beginning.
The content of this article will cover the basic operations of writing files.
Sequential reading and writing of files: starting from the first character each time.
Random reading and writing of files: it can read and write from any location at a time.
2. The order of files
Applicable documents | Function | Function name | Applicable to |
text file | Character input function | fgetc | all input streams |
text file | character output function | fputc | All output streams |
text file | Text line input function | fgets | all input streams |
text file | Text line output function | fputs | All output streams |
text file | Format input function | fscanf | all input streams |
text file | Format output function | fprintf | All output streams |
binary file | binary input | fread | document |
binary file | binary output | fwrite | document |
1. Input stream and output stream
Flow is equivalent to a transitional process. How to determine whether the above function is an input stream or an output stream?
We can look at it from the perspective of memory. If data flows to memory, we can regard it as an input stream. Otherwise, it is the output stream.
Take fgetc and fputc as examples:
2、fputc
1. Function
Write a character character into the stream.
2. Parameters
character: character (the essence of a character is the ASCII code value, which is an integer).
stream: file pointer.
3. Usage
#include<stdio.h>
int main()
{
//以‘只写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
//输入
for (int i = 'a'; i <= 'z'; i++)
fputc(i, pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.Attention
You can only complete this function by whichever method you use to open the file. For example, if you open a file in 'write only' mode, you cannot perform reading operations.
If you are confused about the opening method, please click the link below to view the content of the previous article:
3、fgetc
1. Function
Get data from stream.
2. Parameters
A stream of file pointer type.
3. Usage
If the acquisition fails, the function will return EOF
//以‘只读’的方式打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
//输出
int ch;
while((ch=fgetc(pf))!=EOF)
{
printf("%c ",ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4、fputs
1. Function
Writes a string to the stream.
2. Parameters
str: Parameters that need to be written to the file.
stream: file stream.
3. Usage
If there is no '\n', it will enter the data on one line.
If the file pointer pf is replaced by stdout, the program will print the data on the screen.
#include<stdio.h>
int main()
{
//以‘只写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
fputs("abcd", pf);//不换行写法
fputs("abcd\n", pf);//换行写法
fputs("abcd", stdout);//将数据打印在屏幕上
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
5、fgets
1. Function
Get a string from the stream.
2. Parameters
str: string.
num: The number of reads.
stream: file stream.
3. Usage
If the acquisition fails, the function will return EOF
#include<stdio.h>
int main()
{
//以‘只读’的方式打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[20];
fgets(str, 10, pf);
printf("%s", str);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4. Attention
- The function will only read at most one row of data.
- When there is enough data, the parameter is num, and the function will only read num-1 data.
6、fprintf
1. Function
Write formatted data to a file stream.
2. Parameters
By comparing the parameters of fprintf and printf, you will find that fprintf’s parameter has one more file pointer than printf’s parameter.
3. Usage
#include<stdio.h>
struct S
{
float f;
char ch;
int n;
};
int main()
{
struct S s = { 4.14f,'w',1 };
//以‘只 写’的方式打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
printf("%f %c %d\n", s.f, s.ch, s.n);
fprintf(pf,"%f %c %d\n", s.f, s.ch, s.n);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
7、fscanf
1. Function
Read data from the stream.
2. Parameters
The parameters of fscanf and scanf are similar, with an additional file stream stream.
3. Usage
#include<stdio.h>
struct S
{
float f;
char ch;
int n;
};
int main()
{
struct S s = { 4.14f,'r',1 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
fscanf(pf, "%f %c %d", &(s.f), &(s.ch), &(s.n));
printf("%f %c %d", s.f, s.ch, s.n);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
8、fwrite
1. Function
Write the data in ptr to the stream, the size is size, and the number is count.
2. Parameters
ptr: Pointer to the data to be written to the file stream.
size: The size of each data.
count: number of data.
stream: the file stream to be written.
3. Usage
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[] = "abcd";
fwrite(str, sizeof(char), sizeof(str) / sizeof(str[0]), pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4. Attention
The file opening method must match the function, must be written in binary format.
9、fread
1. Function
Read data from a file stream.
2. Parameters
ptr: Pointer to the location where the data in the file stream is to be stored.
size: The size of each data.
count: number of data.
stream: file stream.
3. Usage
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "rb");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
char str[20];
fread(str, sizeof(char), sizeof(str) / sizeof(str[0]), pf);
printf("%s", str);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4. Attention
The file opening method must match the function,The binary reading method must be used.
3. Random reading and writing of files
Determine the location of the file pointer based on its location and offset.
SEEK_SET | Beginning of file |
SEEK_END | Current position of the file pointer |
SEEK_CUR | End of file |
1、fseek
The function is to change the position of the file pointer.
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
printf("%c\n", ch);
ch = fgetc(pf);//b
printf("%c\n", ch);
ch = fgetc(pf);//c
printf("%c\n", ch);
fseek(pf, -1, SEEK_CUR);
ch = fgetc(pf);
printf("%c\n", ch);//c
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
2、ftell
The function is to return the offset of the current pointer of the file.
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
printf("%c\n", ch);
ch = fgetc(pf);//b
printf("%c\n", ch);
ch = fgetc(pf);//c
printf("%c\n", ch);
int pos = ftell(pf);
printf("%d", pos);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
3、rewind
The function is to return the file pointer to the starting position.
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");//abcdef
if (pf == NULL)
{
perror("fopen");
return 0;
}
//处理
int ch;
ch = fgetc(pf);//a
ch = fgetc(pf);//b
ch = fgetc(pf);//c
int pos = ftell(pf);
printf("%d\n", pos);
rewind(pf);
pos = ftell(pf);
printf("%d", pos);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4. Binary files and text files
According to the storage form of data, files can be divided into binary files and text files.
Data is stored in binary format in a file, which is a binary file.
Data is stored in ASCII code values in files, which are text files.
5. Sign of the end of file reading
During the file reading process, the return value of the feof function cannot be used directly to determine whether the file is ended.
1. Text file reading end mark
fgetc——determine whether it is EOF
fgets——determine whether it is NULL
2. Binary file reading end flag
fread - Determines whether the return value is less than the actual number to be read.
6. File buffer
Thank you for your support!