不错的分享(推荐C++复杂版)
C语言库函数查询手册(C参考手册大全)
在程序设计中,从文件功能的角度分为:程序文件,数据文件
FILE*pf://文件指针变量

//打开文件
FILE* fopen(Const char* filename ,const char* mode);
//关闭文件
int fclose(FILE* stream);
#include
int mian() {
FILE* pf=fopen("test.dat","w");
if(pf==null) {
perror("fopen");
renturn 1;
}
//写文件相关操作
//关闭文件
fclose(pf);
pf=NULL;
renturn 0;
}
| 文件使用方式 | 含义 | 如果指定文件不存在 |
|---|---|---|
| “r”(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
| “w”(只写) | 为了输出数据,打开一个文本文件 | 建立一个新的文件 |
| “a”(追加) | 向文本文件尾添加数据 | 建立一个新的文件 |
| “rb”(只读) | 为了输入数据,打开一个二进制文件 | 出错 |
| “wb”(只写) | 为了输出数据,打开一个二进制文件 | 建立一个新的文件 |
| “ab”(追加) | 向一个二进制文件尾添加数据 | 出错 |
| “r+”(读写) | 为了读和写,打开一个文本文件 | 出错 |
| “w+”(读写) | 为了读和写,建立一个新的文件 | 建立一个新的文件 |
| “a+”(读写) | 打开一个文件,在文件尾进行读写 | 建立一个新的文件 |
| “rb+”(读写) | 为了读和写打开一个二进制文件 | 出错 |
| “wb+”(读写) | 为了读和写,新建一个新的二进制文件 | 建立一个新的文件 |
| “ab+”(读写) | 打开一个二进制文件,在文件尾进行读和 | 建立一个新的文件 |
| 功能 | 函数名 | 适用于 |
|---|---|---|
| 字符输入函数 | fgetc | 所有输入流 |
| 字符输出函数 | fputs | 所有输出流 |
| 文本行输入函数 | fgets | 所有输入流 |
| 文本行输出函数 | fputs | 所有输出流 |
| 格式化输入函数 | fscanf | 所有输入流 |
| 格式化输出函数 | fprintf | 所有输出流 |
| 二进制输入 | fread | 文件 |
| 二进制输出 | fwrite | 文件 |


#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
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);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}


int main() {
char arr[10] = { 0 };
FILE* pf = fopen("test.dat", "w");
if (pf == NULL) {
perror("fopen");
return 1;
}
//写文件
fputs("abcdef\n",pf);
fputs("hdfakgndflkng\n",pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}


int main() {
char arr[10] = { 0 };
FILE* pf = fopen("test.dat", "r");
if (pf == NULL) {
perror("fopen");
return 1;
}
//读文件
fgets(arr,4,pf);
printf("%s\n",arr);
fgets(arr, 4, pf);
printf("%s\n", arr);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}


struct S
{
char arr[10];
int age;
float sc;
};
int main() {
struct S s = { "hi",24,34.5f};
FILE* pf = fopen("test.dat", "w");
if (pf == NULL) {
perror("fopen");
return 1;
}
//写文件
fwrite(&s,sizeof(struct S), 1, pf);
printf("%s %d %f\n",s.arr,s.age,s.sc);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

struct S
{
char arr[10];
int age;
float sc;
};
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.age,s.sc);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}


int main() {
struct S s = {"hi",23,45.7f};
char buf[100] = {0};
//sprintf 把一个格式化的数据,转化成字符串
sprintf(buf,"%s %d %f\n",s.arr,s.age,s.sc);
printf("%s\n",buf);
return 0;
}
struct S
{
char arr[10];
int age;
float sc;
};
int main() {
struct S s = {"hi",23,45.7f};
struct S tmp = {0};
char buf[100] = {0};
//sprintf 把一个格式化的数据,转化成字符串
sprintf(buf,"%s %d %f\n",s.arr,s.age,s.sc);
printf("%s\n",buf);
//从buf字符串中还原出一个结构体数据
sscanf(buf,"%s %d %f",&(tmp.arr),&(tmp.age),(&tmp.sc));
printf("%s %d %f\n",tmp.arr,tmp.age,tmp.sc);
return 0;
}

| 函数 | 详解 |
|---|---|
| scanf | 针对标准输入的格式化的输入语句-stdin |
| fscanf | 针对所有输入流的格式化的输入语句-stdin/文件 |
| sscanf | 从一个字符串中读取一个格式化的数据 |
| printf | 针对标准输出的格式化输出语句 |
| fprintf | 针对所有输出流的格式化输出语句-stdout/文件 |
| sprintf | 把一个格式化的数据,转换成字符 |
根据文件指针的位置和偏移量来定位文件指针
int fseek( FILE *stream, long int offset, int origin );

int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL) {
perror("fopen");
return 1;
}
//读取文件
int ch = fgetc(pf);
printf("%c \n",ch);//a
//调整文件指针
fseek(pf,2,SEEK_CUR);
ch = fgetc(pf);
printf("%c\n",ch);//d
ch = fgetc(pf);
printf("%c\n", ch);//e
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
返回文件指针相对于起始位置的偏移量
long int(FILE* stream);

int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL) {
perror("fopen");
return 1;
}
//读取文件
int ch = fgetc(pf);
printf("%c \n",ch);//a
//调整文件指针
fseek(pf,2,SEEK_CUR);
ch = fgetc(pf);
printf("%c\n",ch);//d
ch = fgetc(pf);
printf("%c\n", ch);//e
int ret = ftell(pf);
printf("%d\n",ret);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL) {
perror("fopen");
return 1;
}
//读取文件
int ch = fgetc(pf);
printf("%c \n",ch);//a
//调整文件指针
fseek(pf,2,SEEK_CUR);
ch = fgetc(pf);
printf("%c\n",ch);//d
ch = fgetc(pf);
printf("%c\n", ch);//e
//返回文件指针相对于起始位置的偏移量
int ret = ftell(pf);
printf("%d\n",ret);
//让文件指针回到起始位置
rewind(pf);
ch=fgetc(pf);
printf("%c\n",ch);//a
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}


fgetq函数在读取结束的时候,会返回EOF
正常读取的时候,返回的是读取到的字符的ASCII码值
fgets函数在读取结束的时候,会返回NULL
正常读取的时候,返回存放字符串的空间起始地址
fread函数在读取的时候,返回的是实际读取到的完整元素的个数
如果发现读取到的完整的元素的个数小于指定的元素个数,这就是最后一次读取
int main() {
FILE* pfread = fopen("test.txt", "r");
if (pfread == NULL) {
perror("fopen");
return 1;
}
FILE* pfwrite = fopen("test2.txt", "w");
if (pfwrite == NULL) {
fclose(pfread);
pfread = NULL;
return 1;
}
//文件打开成功
//读写文件
int ch = 0;
while ((ch = fgetc(pfread)) != NULL)
{
//写文件
fputc(ch,pfwrite);
}
if (feof(pfread)) {
printf("遇到文件结束标志,文件正常结束!\n");
}
else if (ferror(pfread)) {
printf("文件读取失败!\n");
}
//关闭文件
fclose(pfread);
pfread = NULL;
fclose(pfwrite);
pfwrite = NULL;
return 0;
}

