文件操作相关函数及其作用
函数名作用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; }