文件操作函数
★:为什么要使用文件
当程序运行时,数据存放在内存中,当程序退出后,占用的内存空间被回收,数据就不存在了,当我们需要保存程序运行结果时,就需要将数据存放在磁盘文件,存放到数据库。
★:什么是文件
在程序设计中,我们一般谈的文件有两种:程序文件、数据文件(从文件功能的角度来分类的)。
1:程序文件
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。
2:数据文件
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。
3:文件名
一个文件要有一个唯一的文件标识,以便用户识别和引用。
文件名包含3部分:文件路径+文件名主干+文件后缀。
例如: c:\code\test.txt
★:文件的打开和关闭
1:文件指针
缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。
每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名FILE.
2:文件的打开和关闭
文件在读写之前应该先打开文件,在使用结束之后应该关闭文件,ANSIC 规定使用fopen函数来打开文件,fclose来关闭文件。
文件使用方式 | 含义 | 如果指定文件不存在 |
---|---|---|
“r”(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
“w”(只写 ) | 为了输出数据,打开一个文本文件 | 建立一个新的文件`` |
“a”(追加) | 向文本文件尾添加数据 | 建立一个新的文件 |
“rb”(只读) | 为了输入数据,打开一个二进制文件 | 出错 |
“wb”(只写) | 为了输出数据,打开一个二进制文件 | 建立一个新的文件 |
“ab”(追加) | 向一个二进制文件尾添加数据 | 出错 |
“r+”(读写) | 为了读和写,打开一个文本文件 | 出错 |
“w+”(读写) | 为了读和写,建议一个新的文件 | 建立一个新的文件 |
“a+”(读写) | 打开一个文件,在文件尾进行读写 | 建立一个新的文件 |
“rb+”(读写) | 为了读和写打开一个二进制文件 | 出错 |
“wb+”(读写) | 为了读和写,新建一个新的二进制文件 | 建立一个新的文件 |
“ab+”(读写) | 打开一个二进制文件,在文件尾进行读和写 | 建立一个新的文件 |
#include <stdio.h>
int main ()
{
FILE * pFile;
//打开文件
pFile = fopen ("test.txt","w");
//文件操作
if (pFile!=NULL)
{
fputs ("test",pFile);
//关闭文件
fclose (pFile);
}
return 0;
}
3:文件的顺序读写
功能 | 函数名 | 适用于 |
---|---|---|
字符输入函数 | fgetc | 所有输入流 |
字符输出函数 | fputc | 所有输出流 |
文本行输入函数 | fgets | 所有输入流 |
文本行输出函数 | fputs | 所有输出流 |
格式化输出函数 | fscanf | 所有输出流 |
格式化输入函数 | fprintf | 所有输入流 |
二进制输入 | fread | 文件 |
二进制输出 | fwrite | 文件 |
int fgetc(FILE *stream)
函数从输入流stream的当前位置返回下一个字符,并将文件位置指示器增大。该字符作为一个无符号字符读取,并被转换成为一个整型值。
函数的功能:是从文件指针指定的文件中读入一个字符,该字符的ASCII值作为函数的返回值,若返回值为EOF,说明文件结束,EOF是文件结束标志,值为-1。
int main()
{
FILE* pf = fopen("test.txt", "wb");
char test[20];
scanf("%s", test);
fwrite(test, sizeof(char), strlen(test), pf);//写内容到文件
fclose(pf);
pf = NULL;
FILE* pf1 = fopen("test.txt", "rb");
char test1[20];
printf("%s\n", fgets(test1, strlen(test1) + 1, pf1));//读内容显示
fclose(pf1);
pf1 = NULL;
return 0;
}
int fputc (int c, FILE *fp)
正常调用情况下,函数返回写入文件的字符的ASCII码值,出错时,返回EOF(-1)。当正确写入一个字符或一个字节的数据后,文件内部写指针会自动后移一个字节的位置。
函数功能: 将字符c写到文件指针fp所指向的文件的当前写指针的位置。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char test[20] = { "hello world" };
for (int i = 0; i < strlen(test); i++)
{
fputc(test[i], stdout);//将test写入到输出流中
}
return 0;
}
char *fgets(char *str, int n, FILE *stream)
从指定的流 stream 读取一行,并把它存储在str所指向的字符串内。当读取(n-1)个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
int fputs(const char *str, FILE *stream);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE* pf = fopen("test.txt", "wb");
char test[20];
scanf("%s", test);
fputs(test,pf);//保存字符串到文件
fclose(pf);
pf = NULL;
FILE* pf1 = fopen("test.txt", "rb");
char test1[20];
fgets(test1, strlen(test)+1, pf1);//从文件中读取字符串
printf("%s", test1);
return 0;
}
4:对比一组函数
int scanf(const char * restrict format,…);
从标准输入流stdin中读内容,按用户指定的格式从键盘上把数据输入到指定的变量之中。
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
return 0;
}
int fscanf(FILE *stream, char *format[,argument…]);
从一个数据流中读取格式化数据,fscanf遇到空格和换行时结束。
int main()
{
char str[10], str1[10], str2[10];
FILE* pf = fopen("test.txt", "r");
fscanf(pf, "%s %s %s", &str, &str1, &str2);
printf("%s %s %s", str, str1, str2);
fclose(pf);
pf = NULL;
return 0;
}
int sscanf( const char *buffer, const char *format, [ argument ] … );
sscanf读取格式化的字符串中的数据,sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。
int main()
{
char buf[10];
sscanf("123456 ", "%s", buf);
printf("%s\n", buf);
return 0;
}
5:文件的随机读写
int fseek ( FILE * stream, long int offset, int origin );
根据文件指针的位置和偏移量来定位文件指针。
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, 9, SEEK_SET);
fputs(" sam", pFile);
fclose(pFile);
return 0;
}
long int ftell ( FILE * stream );
返回文件指针相对于起始位置的偏移量
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
printf("%d", ftell(pFile));
fclose(pFile);
return 0;
}
void rewind ( FILE * stream );
让文件指针的位置回到文件的起始位置
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
printf("%d\n", ftell(pFile));
rewind(pFile);
printf("%d\n", ftell(pFile));
fclose(pFile);
return 0;
}
6:文件读取结束的判定
在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。
而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。
- 文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets )
例如:
fgetc 判断是否为 EOF .
fgets 判断返回值是否为 NULL . - 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。
例如:
fread判断返回值是否小于实际要读的个数。
#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{
double a[SIZE] = {1.,2.,3.,4.,5.};
FILE *fp = fopen("test.bin", "wb"); // 必须用二进制模式
fwrite(a, sizeof *a, SIZE, fp); // 写 double 的数组
fclose(fp);
double b[SIZE];
fp = fopen("test.bin","rb");
size_t ret_code = fread(b, sizeof *b, SIZE, fp); // 读 double 的数组
if(ret_code == SIZE) {
puts("Array read successfully, contents: ");
for(int n = 0; n < SIZE; ++n) printf("%f ", b[n]);
putchar('\n');
} else { // error handling
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp)) {
perror("Error reading test.bin");
}
}
fclose(fp);
}