文件的顺序读写和随机读写- -文本
fputec- -在文件上写
int main()
{
FILE* pf = fopen("test.dat", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fputc('b', pf);
fputc('i', pf);
fputc('t', pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
- 打开文件看到bit
int main()
{
fputc('b',stdout);
fputc('i',stdout);
fputc('t',stdout);
}
- 在屏幕上输出bit
fputs- -在文件上写一行
int main()
{
FILE* pf = fopen("test.dat", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件-按照行来写
fputs("abcdef\n", pf);
fputs("qwertyuiop\n", pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
fgetc- -读文件
- 使用fgetc从文件流中读取数据
- 读取结束会读取到EOF即-1
int main()
{
FILE* pf = fopen("test.dat", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读文件
int ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
- fgetc从标准输入流读取
int main()
{
int ret = fgetc(stdin);
printf("%c\n", ret);
ret = fgetc(stdin);
printf("%c\n", ret);
ret = fgetc(stdin);
printf("%c\n", ret);
return 0;
}
fgets- -读文件中的一行
int main()
{
char arr[10] = "xxxxxx";
FILE* pf = fopen("test.dat", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读文件
fgets(arr, 4, pf);//其实读的是三个,最后一个放\0
printf("%s\n", arr);
fgets(arr, 4, pf);
printf("%s\n", arr);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件的顺序读写和随机读写- -格式化
fscanf-读文件
fprintf-打印文件内容
结构体的数据
struct S
{
char arr[10];
int num;
float sc;
};
int main()
{
struct S s = {0};
//对格式化的数据进行写文件
FILE* pf = fopen("test.dat", "r");
if (NULL == pf)
{
perror("fopen");
return 1;
}
//读文件
fscanf(pf, "%s %d %f", s.arr, &(s.num), &(s.sc));
//打印
fprintf(stdout, "%s %d %f\n", s.arr, s.num, s.sc);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件的顺序读写和随机读写- -二进制
fwrite- -写文件
struct S
{
char arr[10];
int num;
float sc;
};
int main()
{
struct S s = { "abcde", 10, 5.5f };
//二进制的形式写
FILE*pf = fopen("test.dat", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fwrite(&s, sizeof(struct S), 1, pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
fread读取
struct S
{
char arr[10];
int num;
float sc;
};
fread- -读文件
int main()
{
struct S s = {0};
//二进制的形式读
FILE*pf = fopen("test.dat", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读文件
fread(&s, sizeof(struct S), 1, pf);
printf("%s %d %f\n", s.arr, s.num, s.sc);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
和文件的顺序读写和随机读写一起使用
fseek
-
三个参数:1、针对哪个流
2、偏移量(负数-向左偏移,正数-向右偏移)
3、起始位置 -
三个起始位置:
-
rewind 让文件指针回到其实位置
fseek、rewind运用:
- 先在写代码的那个文件中创建一个test.txt的文本文件
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读取文件
int ch = fgetc(pf);
printf("%c\n", ch);//a- 读完a文件指针指向b
//调整文件指针
fseek(pf, -2, SEEK_END);//文件末尾-偏移一个指向 f ,偏移两个指向 e
ch = fgetc(pf);
printf("%c\n", ch);//e
ch = fgetc(pf);
printf("%c\n", ch);//f
int ret = ftell(pf);
printf("%d\n", ret);
//让文件指针回到其实位置
rewind(pf);
ch = fgetc(pf);
printf("%c\n", ch);//a
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件- -读 、写的比较
sprintf、sscanf
- sprintf 把一个格式化的数据,转换成字符串
- sscanf从buf字符串中还原出一个结构体数据
#include <stdio.h>
struct S
{
char arr[10];
int age;
float f;
};
int main()
{
struct S s = { "hello", 20, 5.5f };
struct S tmp = { 0 };
char buf[100] = {0};
//sprintf 把一个格式化的数据,转换成字符串
sprintf(buf, "%s %d %f", s.arr, s.age, s.f);
printf("%s\n", buf);
//从buf字符串中还原出一个结构体数据
sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.f));
printf("%s %d %f\n", tmp.arr, tmp.age, tmp.f);
return 0;
}
文件- -文本和二进制比较
- 根据数据的组织形式,数据文件被称为文本文件或者二进制文件。
- ascall- -文本文件
- 二进制形式- -二进制文件
- 字符一律以ASCII形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储。如有整数10000,如果以ASCII码的形式输出到磁盘,则磁盘中占用5个字节(每个字符一个字节),而二进制形式输出,则在磁盘上只占4个字节(VS2013测试)
//0000 0000 0000 0000 0010 0111 0001 0000
//00 00 27 10 - 在vs上添加文本文件,以二进制的形式打开看
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fwrite(&a, sizeof(int), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
文件- -读取结束
**写代码把test.txt文件拷贝一份,生成test2.txt
**
int main()
{
FILE* pfread = fopen("test.txt", "r");
if (pfread == NULL)
{
return 1;
}
FILE* pfwrite = fopen("test2.txt", "w");
if (pfwrite == NULL)
{
fclose(pfread);
pfread = NULL;
return 1;
}
//文件打开成功
//读写文件
int ch = 0;
while ((ch = fgetc(pfread)) != EOF)
{
//写文件
fputc(ch, pfwrite);
}
if (feof(pfread))
{
printf("遇到文件结束标志,文件正常结束\n");
}
else if(ferror(pfread))
{
printf("文件读取失败结束\n");
}
//关闭文件
fclose(pfread);
pfread = NULL;
fclose(pfwrite);
pfwrite = NULL;
return 0;
}