文章目录
- 1.概念
- 1.1定义:
- 1.2分类
- 1.3文件名
- 2.文件的使用
- 2.1文件指针
- 2.2开闭函数
- 2.3顺序读写
- 2.3.1何为读写
- 2.3.2读写函数
- 1.字符输出fputc(输出到文件 写到文件中)
- 2.字符输入fgetc(输入到程序 读到程序中)
- 3.文本行函数
- 4.格式化函数
- 4.1...scanf--printf
- 4.2...fscanf--fprintf
- 4.3...sscanf--sprintf
- 5.二进制函数
1.概念
1.1定义:
文件:电脑文件,计算机文件。存储在长期储存设备(磁盘、光盘、磁带)或临时存储设备(计算机内存)中的一段数据流,归属于计算机文件系统管理下。
存储于长期存储设备的文件:
1.长期存储
2.程序或系统运行产生的临时数据,于程序或系统退出后删除。
1.2分类
程序文件:
源程序文件(.c)
目标文件(.obj)
可执行程序(.exe)。
数据文件:
程序
程序运行时读写的数据
程序运行读取数据或输出内容的文件
1.3文件名
文件存在的标识,操作系统根据文件名对其进行控制和管理。不同操作系统对文件命名规则略有不同,文件名的格式和长度因系统而异。为方便区分计算机中不同文件,给每个文件设定指定名称。
组成:
文件路径 + 文件名主干 + 文件后缀
C:\code2023\ test4_29 .txt
2.文件的使用
2.1文件指针
指向文件信息区(文件在内存中开辟的一个FILE结构体变量)的指针变量
2.2开闭函数
#include<stdlib.h>
打开:
FILE *fopen( const char *filename, const char *mode );
关闭:
int fclose( FILE *stream );
示例:
#include <stdio.h>
int main(void)
{
FILE* pf = fopen("test.dat", "w");
FILE* pf = fopen("C:\\code2023\\test4_29.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fclose(pf);
pf = NULL;
return 0;
}
- 程序能够打开的文件有限,文件属于一种资源。如果不释放,文件会被占用。可能会导致一些操作被缓冲在内存中,缓冲在内存中的数据不能正常写入文件中导致数据丢失。
2.3顺序读写
2.3.1何为读写
程序位于内存
数据位于硬盘上的文件
文件拿出数据:输入/读取(r)
程序存到文件:输出/写入(w)
2.3.2读写函数
1.字符输出fputc(输出到文件 写到文件中)
int fputc( int c, FILE *stream );
无符号字符输出到流
#include <stdio.h>
int main(void)
{
写文件-->程序输出到文件
w:无此文件创立文件
覆盖文件原有内容
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fputc('a', stdout);
fclose(pf);
pf = NULL;
return 0;
}
2.字符输入fgetc(输入到程序 读到程序中)
int fgetc( FILE *stream );
从流中读取字符
成功返回ASCII值
失败返回EOF
#include <stdio.h>
int main(void)
{
FILE* pf = fopen("test.txt", "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);
int ret = fgetc(stdin);
fclose(pf);
pf = NULL;
return 0;
}
3.文本行函数
输出:
int fputs( const char *string, FILE *stream );
fputs("abcdef\n", pf);
输入:
char *fgets( char *string, int n, FILE *stream );
fgets(arr, 4, pf);
点击 fgets函数详解 对fgets函数有更深刻的认识
4.格式化函数
4.1…scanf–printf
输出:
int printf( const char *format [, argument]... );
printf("%d", a);
printf("Hello,World!\n");
输入:
int scanf( const char *format [,argument]... );
scanf("%d %d", &a, &b);
4.2…fscanf–fprintf
输出:
int fprintf( FILE *stream, const char *format [, argument ]...);
fprintf(pf, "%s %d %f", p1.name, p1.dpi, p1.sens);
输入:
int fscanf( FILE *stream, const char *format [, argument ]... );
fscanf(pf, "%s %d %f", p1.name, &(p1.dpi), &(p1.sens));
4.3…sscanf–sprintf
输出:
int sprintf( char *buffer, const char *format [, argument] ... );
输入:
int sscanf( const char *buffer, const char *format [, argument ] ... );
示例:
#include <stdio.h>
struct S
{
char arr[10];
int age;
float f;
};
int main()
{
struct S s = { "hello", 20, 3.14f };
struct S tmp = { 0 };
char buffer[100] = { 0 };
sprintf(buffer, "%s %d %f", s.arr, s.age, s.f);
printf("%s\n", buffer);
sscanf(buffer, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.f));
printf("%s %d %f\n", tmp.arr, tmp.age, tmp.f);
return 0;
}
scanf 标准格式化输入 - stdin
fscanf 所有输出流格式化输出- stdin / 文件
sscanf 从字符串中读取格式化数据
printf 标准格式化输出 - stdout
fprintf 所有输出流格式化输出 - stdout /文件
sprintf 格式化数据转换成字符串
5.二进制函数
输出:
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
fwrite(&s, sizeof(struct S), 1, pf);
输入:
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
fread(&s, sizeof(struct S), 1, pf);
点击 阿猿的《通讯录》查看通讯录,可以对上述函数又更清晰的理解