为什么操作文件
为了让数据达到持久化,我们可以把数据存放在磁盘文件、存放到数据库等方式。
这里所说的文件指的是磁盘上的文件
文件指针
在学习文件操作之前我们需要知道文件指针的概念
文件指针就是文件类型的指针。
每个被使用的文件都在内存中开辟了一个相应的文件信息区。
文件信息区用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)
这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名FILE
需要注意的是: 1、每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息, 使用者不必关心细节。 2、一般都是通过一个FILE的指针来维护这个FILE结构的变量,这样使用起来更加方便。
文件的打开与关闭
FILE * fopen ( const char * filename, const char * mode );
//如果文件打开失败,会返回NULL
if(pf == NULL)
{
perror("fopen");
return 1;
}
int fclose ( FILE * stream );
文件的打开方式(部分)
FILE* pf = fopen ("test.txt","w");
//如果文件打开失败,会返回NULL
if(pf == NULL)
{
perror("fopen");
return 1;
}
//文件操作
fclose (pf);
pf = NULL;
文件的顺序读写
int fputc ( int character, FILE * stream );
功能:Write character to stream
返回值:成功时,将返回所写的字符。 如果发生写入错误,将返回EOF,并设置错误指示器(ferror)。
//下述代码的作用是往test.txt文本中写入26个大写字符
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
int main()
{
//打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen"); //perror函数自动在字符串后面追加空格和冒号以及错误信息
//printf("%s\n", strerror(errno));
return 0;
}
//写文件
for (int i = 0; i < 26; i++)
{
fputc('A' + i, pf);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int fgetc ( FILE * stream );
功能:Get character from stream
返回值:成功后,将返回字符读取(提升为 int 值)。 返回类型为 int 以适应特殊值 EOF,这表示失败: 如果位置指示器位于文件末尾,则该函数返回 EOF 并设置流的 eof 指示器 (feof)。 如果发生其他读取错误,该函数还会返回 EOF,但改为设置其错误指示器 (ferror)。
#include <stdio.h>
#include <string.h>
int main()
{
//打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 0;
}
//读文件
for (int i = 0; i < 30; i++)
{
int ch = fgetc(pf);
printf("%c ", ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int fputs ( const char * str, FILE * stream );
功能:Write string to stream
返回值:一旦成功,将返回一个非负值。 出错时,该函数返回EOF并设置错误指示符(ferror)。
#include <stdio.h>
#include <string.h>
int main()
{
//打开文件
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror(fopen);
return 0;
}
//写文件
fputs("Hello world!\n", pf);
fputs("Good Good study", pf);
fputs(" day day up\n", pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
char * fgets ( char * str, int num, FILE * stream );
功能:Get string from stream
返回值:成功后,函数返回 str。 如果在尝试读取字符时遇到文件末尾,则设置 eof 指示器 (feof)。如果在读取任何字符之前发生这种情况,则返回的指针为空指针(str 的内容保持不变)。 如果发生读取错误,则设置错误指示器(ferror),并返回空指针(但str指向的内容可能已更改)。
参数num:要复制到 str 的最大字符数(包括终止空字符)。
#include <stdio.h>
#include <string.h>
int main()
{
//打开文件
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror(fopen);
return 0;
}
//读文件
char str[200];
fgets(str, 200, pf);
printf("%s", str);
fgets(str, 200, pf);
printf("%s", str);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int fprintf ( FILE * stream, const char * format, ... );
功能:Write formatted data to stream
返回值:成功后,将返回写入的字符总数。 如果发生写入错误,则设置错误指示器(ferror)并返回负数。 如果在写入宽字符时发生多字节字符编码错误,errno 将设置为 EILSEQ 并返回负数。
//下述代码的作用是写入格式化数据到文件中
struct S
{
char name[20];
int age;
float Weight;
};
int main()
{
struct S s = { "张三", 20, 121.6f };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//格式化写文件
fprintf(pf, "%s %d %lf", s.name, s.age, s.Weight);
fclose(pf);
pf = NULL;
}
int fscanf ( FILE * stream, const char * format, ... );
功能:Read formatted data from stream
//下述代码的功能是从文件中读取带格式的数据到结构中
struct S
{
char name[20];
int age;
float Weight;
};
int main()
{
struct S s = { 0 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//格式化读文件
fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.Weight));
printf("%s %d %lf", s.name, s.age, s.Weight);
fclose(pf);
pf = NULL;
return 0;
}