前言
关于C语言的文件顺序读写操作有许多的函数需要我们学习和了解,下面就让我们一起来看看这些函数的使用吧!
目录
文件操作函数汇总:
1.fputc
1.1原型
fputc函数是向一个流(文件流)中输入一个字符。函数返回的是被输入的字符的ASCII码值。
1.2使用
#include<stdio.h>
int main()
{
FILE* pf = fopen("text.txt", "w");//打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
//fputc('c', pf);
char i = 'a';
for (i='a'; i <= 'z'; i++)//将字符a-z输入到文件中
{
fputc(i, pf);
}
//写文件
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
2.fgetc
2.1原型
fgetc函数是从一个流(文件)中读取一个字符,读取成功返回字符的ASCII码值,读取失败返回NULL。
2.2使用
#include<stdio.h>
int main()
{
FILE* pf = fopen("text.txt", "r");//打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
//fputc('c', pf);
int c = 0;
while ((c = fgetc(pf)) != EOF)//从文件中获取字符
{
printf("%c ", c);
}
//写文件
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
3.fputs
3.1原型
写一个字符串到文件中。
3.2使用
#include<stdio.h>
int main()
{
FILE* pf = fopen("text.txt", "w");//打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
fputs("hello world\n", pf);//写一个字符串到文件中
fputs("hello world\n", pf);
//写文件
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
注意如果写进去想要达到换行的效果需要加换行符。
4.fgets
4.1原型
fgets是从文件中读取一个字符串放到字符数组中。参数n是最多读取的个数。如果读取成功返回字符串的地址,读取失败返回NULL。
4.2使用
#include<stdio.h>
int main()
{
FILE* pf = fopen("text.txt", "r");//打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
char buf[20] = { 0 };
fgets(buf, 14, pf);//从文件中读取字符
printf("%s", buf);
//写文件
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
注意:fgets从文件中读取字符时会自动在读取的字符末尾加上\0,比如读取4个字符那么最后一个字符就是\0。
5.fprintf
5.1原型
fprintf函数是格式化的方式将数据写到文件中。
5.2使用
#include<stdio.h>
struct stu
{
int _age;
float _weight;
char _id[20];
};
int main()
{
FILE* pf = fopen("text.txt", "w");//以写的方式打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
struct stu s1 = { 20,6.6,"1234567" };
fprintf(pf, "%d %f %s", s1._age, s1._weight, s1._id);
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
6.fscanf
6.1原型
从文件中读出一个格式化的数据。
6.2使用
#include<stdio.h>
struct stu
{
int _age;
float _weight;
char _id[20];
};
int main()
{
FILE* pf = fopen("text.txt", "r");//以写的方式打开文件
if (pf == NULL)
{
perror("fopen");
return 0;
}
struct stu s1 = { 20,6.6,"1234567" };
struct stu s2 = { 0 };
fscanf(pf, "%d %f %s", &s2._age, &s2._weight, s2._id);
printf("%d %f %s", s2._age, s2._weight, s2._id);//从文件中获取格式化的数据
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
7.fwrite
7.1原型
fwrite函数是以二进制的形式将内存中的数据写到文件中。
Return Value
fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.
返回值是返回写如元素个数。
Parameters
buffer
Pointer to data to be written
内存空间是要被写的内存地址。
size
Item size in bytes
size是元素的大小。
count
Maximum number of items to be written
count是一次写的数据的个数。
stream
Pointer to FILE structure
stream是指向文件的指针。
7.2使用
#include<stdio.h>
int main()
{
FILE* pf = fopen("text.txt", "wb");//以二进制写的方式打开文件
if (NULL == pf)
{
perror(fopen);
return 1;
}
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
fwrite(arr, sizeof(int), 10, pf);//将数组arr中的内容写到文件中
fclose(pf);
pf = NULL;
return 0;
}
8.fread
8.1原型
fread函数是从文件中读数据到指定的内存中。
Return Value
返回值是返回从文件中读出的元素个数。
buffer:
内存空间是要存放读出数据的内存地址。
size:
size是元素的大小。
count:
count是一次读出数据的个数。
stream:
stream是指向文件的指针。
8.2使用
int main()
{
FILE* pf = fopen("text.txt", "rb");//以二进制读的方式打开文件
if (NULL == pf)
{
perror(fopen);
return 1;
}
int arr[10] = {0 };
int i = 0;
while (fread(&arr[i], 4, 1, pf))//从文件中读出数据
{
i++;
}
i = 0;
while (i < 10)
{
printf("%d ", arr[i]);//打印数组arr
i++;
}
fclose(pf);
pf = NULL;
return 0;
}