文件操作相关函数及其作用
| 函数名 | 作用 |
|---|---|
| fopen | 打开文件 |
| fclose | 关闭文件 |
| fgetc | 字符输入 |
| fputc | 字符输出 |
| fgets | 文本行输入 |
| fputs | 文本行输出 |
| fscanf | 格式化输入 |
| fprintf | 格式化输出 |
| fread | 二进制输入 |
| fwrite | 二进制输出 |
| fseek | 根据文件指针的位置和偏移量定位指针 |
| ftell | 返回文件指针相对初始位置的偏移量 |
| rewind | 让文件指针的位置回到文件的起始位置 |
| feof | 文件的结束判定 |
文件的打开方式:
r:为了输入数据,打开一个已经存在的文本文件
“w”(只写) 为了输出数据,打开一个文本文件 建立一个新的文件
“a”(追加) 向文本文件尾添加数据
“rb”(只读) 为了输入数据,打开一个二进制文件
“wb”(只写) 为了输出数据,打开一个二进制文件 建立一个新的文件
“ab”(追加) 向一个二进制文件尾添加数据
“r+”(读写) 为了读和写,打开一个文本文件
“w+” (读写)为了读和写,建立一个新的文件
“a+” (读写)打开一个文件,在文件尾进行读写
“rb+” (读写)为了读和写打开一个二进制文件
“wb+”(读写)为了读和写,建立一个新的二进制文件
“ab+”(读写)打开一个文件,在二进制文件尾进行读写
| 函数名 | 适用范围 |
|---|---|
| fgetc | 所有输入流 |
| fputc | 所有输出流 |
| fgets | 所有输入流 |
| fputs | 所有输出流 |
| fscanf | 所有输出流 |
| fprint | 所有输出流 |
| fread | 文件 |
| fwrite | 文件 |
文件函数的内容
1. fclose的函数体
int fclose(FILE* stream);
返回值类型: 整型
参数: 要关闭的流(文件)
2. fopen的函数体
FILE * fopen(const char* filename, const char* mode);
返回值类型: 文件指针类型
参数: 要打开的文件名,以什么模式打开。如果打开失败,返回NULL
3. ftell的函数体
long int ftell(FILE* stream);
返回值类型: 长整型
参数: 移到偏移量
4. fseek的函数体
int fseek(FILE* stream,long int offset,int origin);
返回值类型: 整型
参数: 从起始开始偏移
5. rewind的函数体
void rewind(FILE* stream);
返回值类型: 空
参数: 回到当前位置
6. fgetc的函数体
int fgetc(FILE *stream);
返回值类型: ASCII码值(整型)
参数: 读一个流的字符
7. fgets的函数体
char *fgets(char *string , int size, FILE *stream);
返回值类型: 字符指针(先判断返回值是否为EOF或NULL)
参数: 从流里读取最多个数的字符串
8. fputc的函数体
int fputc(int c, FILE *stream);
返回值类型: 整型
参数: 写一个字符到一个流(文件)
9. fputs的函数体
int fputs(const char *string, FILE *stream);
返回值类型: 整型
参数: 写一个字符串到流
10. fread的函数体(二进制的读)
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
返回值类型: 无符号的整型
参数: 读取流的数据的大小个数,存到某处
11. fwrite的函数体(二进制的写)
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
返回值类型: 无符号整型
参数: 从某处将含有多少字节数的数据的个数写进流
12. feof的函数体
int feof(FILE *stream);
返回值类型: 整型
参数: 判断getc是否为EOF,gets是否为NULL(正常结束)
13. fprintf的函数体
int fprintf(FILE* stream, const char* format,[argument]);
返回值类型: 整型
参数: 把数据以某特定的格式写入流
14. fscanf的函数体
int fscanf(FILE* stream,const char* format,[argument]);
返回值类型: 整型
参数: 把数据以某种特定的格式从流读出来
代码如下:
#include <stdio.h>
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);
pf = NULL;
return 0;
}
#include <errno.h>
#include <string.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "r");
int ch = 0;
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "r");
int ch = 0;
char buf[20] = {0};
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
fgets(buf, 20, pf);
printf("%s", buf);
fgets(buf, 20, pf);
printf("%s", buf);
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "w");
int ch = 0;
char buf[20] = {0};
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
fputs("hello bit\n", pf);
fputs("hello bit\n", pf);
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
char name[20];
int age;
float score;
};
int main()
{
struct S s = {0};
FILE* pf = fopen("test.txt", "r");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.score));
printf("%s %d %f\n", s.name, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
struct S s = {"zhou", 20, 59.5f};
FILE* pf = fopen("test.txt", "w");
int ch = 0;
char buf[20] = {0};
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
fprintf(pf, "%s %d %f", s.name, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
char name[20];
int age;
float score;
};
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
int main()
{
struct S s = {"zhou", 20, 59.5f};
FILE* pf = fopen("test.txt", "wb");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
fwrite(&s, sizeof(s), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
int main()
{
struct S s = {0};
FILE* pf = fopen("test.txt", "rb");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//二进制读文件
fread(&s, sizeof(s), 1, pf);
printf("%s %d %f\n", s.name, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
stdin stdout stderr FILE*
int main()
{
struct S s = {0};
fscanf(stdin, "%s %d %f", s.name, &(s.age), &(s.score));
//fprintf(stdout, "%s %d %f\n", "周", 20, 59.5f);
fprintf(stdout, "%s %d %f\n", s.name, s.age, s.);
return 0;
}
int sprintf( char *buffer, const char *format [, argument] ... );
int main()
{
struct S s = {"周", 20, 59.5f};
struct S tmp = {0};
char buf[30] = {0};
//把结构体转换成字符串
sprintf(buf, "%s %d %f", s.name, s.age, s.score);
printf("字符串: %s\n", buf);
sscanf(buf, "%s %d %f", tmp.name, &(tmp.age), &(tmp.score));
printf("结构体: %s %d %f\n", tmp.name, tmp.age, tmp.score);
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "r");
int ch = 0;
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
ch = fgetc(pf);
printf("%c\n", ch);
fseek(pf, -3, SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
printf("%d\n", ftell(pf));//
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
FILE* pf = fopen("test.txt", "r");
int ch = 0;
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
while ((ch = fgetc(pf)) != EOF)
{
printf("%c\n", ch);
}
//feof
fclose(pf);
pf = NULL;
return 0;
}
8575

被折叠的 条评论
为什么被折叠?



