切换主题
C语言文件操作
C语言的文件操作是通过标准库提供的函数来进行的,主要是使用 stdio.h
头文件中的函数进行文件的打开、读取、写入和关闭。C语言对文件的操作分为两类:文本文件和二进制文件。
1. 文件操作基础
在 C 语言中,文件操作的基本步骤通常包括以下几个步骤:
- 打开文件:使用
fopen()
函数打开文件,并返回一个文件指针。 - 读取/写入文件:使用不同的函数读取或写入文件内容。
- 关闭文件:使用
fclose()
函数关闭文件,释放相关资源。
2. 打开文件
fopen()
函数用于打开文件,它返回一个文件指针,之后可以使用该指针来进行文件的读写操作。函数原型如下:
c
FILE *fopen(const char *filename, const char *mode);
1
- filename:要打开的文件的路径。
- mode:文件的打开模式,决定了文件的读写方式。
常用的文件打开模式有:
模式 | 描述 |
---|---|
"r" | 以只读方式打开文件,文件必须存在。 |
"w" | 以写入方式打开文件,如果文件存在会被清空,不存在则创建。 |
"a" | 以追加方式打开文件,文件不存在则创建。 |
"rb" | 以二进制只读方式打开文件。 |
"wb" | 以二进制写入方式打开文件。 |
"ab" | 以二进制追加方式打开文件。 |
"r+" | 以可读可写方式打开文件,文件必须存在。 |
"w+" | 以可读可写方式打开文件,若文件存在则清空,不存在则创建。 |
"a+" | 以可读可写方式打开文件,若文件不存在则创建。 |
"rb+" | 以二进制可读可写方式打开文件。 |
"wb+" | 以二进制可读可写方式打开文件。 |
3. 读取文件
3.1 fgetc()
函数
fgetc()
用于从文件中读取一个字符,返回读取的字符。如果遇到文件末尾(EOF),则返回 EOF
。
c
int fgetc(FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("File open failed");
return 1;
}
char ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // 打印字符
}
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3.2 fgets()
函数
fgets()
用于从文件中读取一行文本,直到遇到换行符、文件末尾或指定的字符数为止。
c
char *fgets(char *str, int n, FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("File open failed");
return 1;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer); // 打印每行文本
}
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3.3 fread()
函数
fread()
用于从文件中读取指定数量的字节,适用于二进制文件。
c
size_t fread(void *ptr, size_t size, size_t count, FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.bin", "rb");
if (file == NULL) {
perror("File open failed");
return 1;
}
char buffer[256];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) {
// 处理读取的二进制数据
}
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4. 写入文件
4.1 fputc()
函数
fputc()
用于将一个字符写入文件。
c
int fputc(int ch, FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
perror("File open failed");
return 1;
}
fputc('A', file); // 写入字符 'A'
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
4.2 fputs()
函数
fputs()
用于将一行字符串写入文件。
c
int fputs(const char *str, FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
perror("File open failed");
return 1;
}
fputs("Hello, World!\n", file); // 写入字符串
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
4.3 fwrite()
函数
fwrite()
用于将指定数量的字节写入文件,适用于二进制文件。
c
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.bin", "wb");
if (file == NULL) {
perror("File open failed");
return 1;
}
int data = 123;
fwrite(&data, sizeof(int), 1, file); // 写入一个整数
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
5. 文件指针操作
5.1 fseek()
函数
fseek()
用于设置文件指针的位置。
c
int fseek(FILE *file, long offset, int whence);
1
offset:偏移量。
whence
:相对于文件的哪个位置,通常使用:
SEEK_SET
:文件开头。SEEK_CUR
:当前文件指针位置。SEEK_END
:文件末尾。
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("File open failed");
return 1;
}
fseek(file, 10, SEEK_SET); // 将文件指针移动到文件开头后的第10个字节
char ch = fgetc(file);
printf("%c\n", ch);
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
5.2 ftell()
函数
ftell()
用于返回当前文件指针的位置。
c
long ftell(FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("File open failed");
return 1;
}
fseek(file, 5, SEEK_SET); // 将文件指针移动到第5个字节
long position = ftell(file);
printf("Current position: %ld\n", position);
fclose(file);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
6. 关闭文件
完成文件操作后,需要关闭文件以释放资源。使用 fclose()
函数关闭文件:
c
int fclose(FILE *file);
1
示例代码:
c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("File open failed");
return 1;
}
// 执行文件操作
fclose(file); // 关闭文件
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
7. 文件操作的错误处理
在文件操作中,错误处理非常重要。常见的错误处理方法有:
- 使用
perror()
输出错误信息。 - 使用
feof()
检查是否已到达文件末尾。 - 使用
ferror()
检查是否发生了文件操作错误。
示例:
c
if (ferror(file)) {
perror("File operation failed");
}
1
2
3
2
3
总结
C语言的文件操作非常灵活,可以处理文本文件和二进制文件。通过熟练掌握 fopen()
、fgetc()
、fputs()
、fread()
、fwrite()
等函数,可以进行各种文件操作。在进行文件读写时,确保文件指针正确移动,并注意及时关闭文件以避免资源泄露。