文件操作
文件基本操作
读写方式 | 作用 |
---|---|
r:read ( fopen(“xx”,“r”)) | 读的方式打开文件,如果不存在打开失败,fopen返回空 |
w:write ( fopen(“xx”,“w”)) | 写的方式,如果文件不存在,创建文件,如果存在,清空原文件 |
a: append | 追加方式打开文件,如果原为有东西,在文件后面接着写,不存在就创建文件 |
r+ | 可读可写,如果文件不存在打开失败 |
w+ | 可读可写,如果文件不存在会自动创建一个文件 |
a+ | 可读可写,如果文件不存在会打开失败,如果文件存在会在文件后面接着写(追加) |
rb | 以二进制读打开文件 |
wb | 以二进制写打开文件 |
ab | 以二进制追加打开文件 |
rb+,wb+,ab+ | 二进制的读写与追加 |
简单的文件操作
- 用什么方式写的,就可以用什么方式读出来,和文件后缀没关系
#include <stdio.h>
int main()
{
FILE* fp = fopen("test.txt", "r");
//FILE* fp = fopen("test.txt", "w");
//FILE* fp = fopen("test.txt", "a");
if(fp==NULL)
{
printf("文件打开失败!\n");
return 0;
}
fclose(fp); //不可以关闭一个空的文件
return 0;
}
字符字符串的读写操作
字符读写操作
-
fgetc: 字符读
-
fputc: 字符写
-
文件结束标记: EOF
-
文件读写函数会完成文件指针移动
#include <stdio.h> int main() { FILE* fp = fopen("read.txt", "r"); int userKey = 0; userKey = fgetc(fp); //以字符的方式读文件 while (userKey != EOF) { putchar(userKey); userKey = fgetc(fp); } fclose(fp); //以字符的方式写文件 FILE* write = fopen("read.txt", "a"); char str[] = { "你是一个好人!" }; int i = 0; while (str[i] != '\0') { fputc(str[i], write); i++; } fclose(write); return 0; }
字符串读写操作
-
fgets: 字符串读
-
fputs:字符串写
#include <stdio.h> void deleteEnter(char* str) { int i = 0; while (str[i] != '\0') i++; str[i - 1] = str[i]; } int main() { //以字符串的方式操作 //fseek ftell printf("\n"); char readStr[100][100]; FILE* readByStr = fopen("str.txt", "r"); //feof(readByStr); ||判断文件指针是否在文件末尾 int row = 0; while (fgets(readStr[row], 100, readByStr)) { //注意点:每一个读出来的字符串末尾是\n /* fgets(readStr[row], 100, readByStr) if(feof(readByStr)) { break; } */ deleteEnter(readStr[row]); //自己手动去掉读出来的\n puts(readStr[row]); //printf("%s", readStr[row]); row++; } fclose(readByStr); FILE* writeByStr = fopen("str.txt", "a"); char strInput[] = "编程真有趣!!!"; fputs(strInput, writeByStr); fclose(writeByStr); return 0; }
格式化读写操作
-
具有表格特性
-
fprint : 文件写操作
-
stdout : 标准输出
-
fscanf :文件读操作
#include <stdio.h> /* fprintf(FILE* fp,"格式控制字符",变量名表); stdout: fscanf(FILE* fp,"格式控制字符",变量名地址表); */ struct MM { char name[20]; int age; int num; }; void writeInfoToFile(struct MM array[], int arrayNum, const char* fileName) { FILE* fp = fopen(fileName, "w"); for (int i = 0; i < arrayNum; i++) { fprintf(fp, "%s\t%d\t%d\n", array[i].name, array[i].age, array[i].num); } fclose(fp); } void printMM(struct MM array[], int arrayNum) { printf("姓名\t年龄\t编号\n"); for (int i = 0; i < arrayNum; i++) { printf("%s\t%d\t%d\n", array[i].name, array[i].age, array[i].num); } } void readInfoFromFile(struct MM array[], const char* fileName) { FILE* fp = fopen(fileName, "r"); int i = 0; while (fscanf(fp, "%s\t%d\t%d\n",array[i].name, &array[i].age, &array[i].num) != EOF) { i++; } fclose(fp); } int main() { struct MM array[3] = { "baby",18,1001, "anning",20,1003, "susu",19,1005 }; writeInfoToFile(array, 3, "mm.txt"); struct MM readMM[3]; readInfoFromFile(readMM, "mm.txt"); printMM(readMM,3); return 0; }
字节流形式读写操作——二进制读写操作
-
fread() :读
-
fwrite() :写
-
size_t fwrite(void const* _Buffer, size_t _ElementSize, size_t _ElementCount, FILE* _Stream);
-
const* _Buffer :要写的内容的首地址
-
size_t _ElementSize : 写多少
-
size_t _ElementCount :写几次
-
FILE* _Stream :写到那个文件中去
#include <stdio.h> struct MM { char name[20]; int age; int num; }; int main() { struct MM array[3] = { "Moying",18,1001, "Liqi",20,1003, "Qiangsen",19,1005 }; FILE* fp = fopen("DK.txt", "wb"); //第一种写法: //for (int i = 0; i < 3; i++) //{ // fwrite(&array[i], sizeof(struct MM), 1, fp); //} fwrite(array, sizeof(struct MM), 3, fp); fclose(fp); FILE* read = fopen("mm.txt","rb"); struct MM readArray[3]; //for (int i = 0; i < 3; i++) //{ // fread(readArray + i, sizeof(struct MM), 1, read); //} fread(readArray, sizeof(struct MM), 3, read); for (int i = 0; i < 3; i++) { printf("%s\t%d\t%d\n", readArray[i].name, readArray[i].age, readArray[i].num); } fclose(read); char str[40000] = ""; //大文件的读写一定是动态内存申请 FILE* readPic = fopen("xx.jpg", "rb"); fread(str, 34937, 1, readPic); fclose(readPic); FILE* writePic = fopen("xxoo.jpg", "wb"); //for (int i = 1000; i < 2000; i++) //{ // str[i] -= 1; //} fwrite(str, 34937, 1, writePic); fclose(writePic); return 0; }
-
文件指针操作
-
ftell :统计文件指针移动的字节数
-
fseek :定位文件指针
-
fseek(FILE* fp,long size,int mode)
- *fp : 文件指针
- long size :偏移的字节数
- int mode :参照点(枚举类型)
- SEEK_CUR 当前位置
- SEEK_END 结束位置
- SEEK_SET 开始位置
-
feof(FILE* fp) :判断文件指针是否在文件末尾
-
rewind(FILE* fp) :把文件指针恢复文件开始位置
#include <stdio.h> int getFileSize(const char *fileName) { FILE* fp = fopen(fileName, "r"); //文件开始的位置 if (fp == NULL) return 0; fseek(fp, 0L, SEEK_END); //移动到文件末尾 int size = ftell(fp); fclose(fp); return size; } int main() { printf("%d\n", getFileSize("xxoo.exe")); int size = getFileSize("xxoo.exe"); char* pMemory = (char*)malloc(size + 1); FILE* read = fopen("xxoo.exe", "r"); fread(pMemory, size, 1, read); fclose(read); //以字符的方式写文件 FILE* fp = fopen("read.txt", "w+"); char str[] = { "你是一个好人!" }; int i = 0; while (str[i] != '\0') { fputc(str[i], fp); i++; } rewind(fp); int userKey = 0; userKey = fgetc(fp); //以字符的方式读文件 while (userKey != EOF) { putchar(userKey); userKey = fgetc(fp); } fclose(fp); return 0; }