目录
FILE文件基本操作
打开文件流的函数:fopen()
关闭文件流的函数:fclose()
查看文件帮助文档 了解参数:
用r只读模式打开文件流:
FILE* readFile = fopen("d:\\1.txt","rb");
如果文件打开成功,关闭文件流并释放文件指针:
if (readFile != NULL)//如果文件打开成功
{
fclose(readFile);//关闭文件流。 类似于free释放
readFile=NULL;//释放文件指针。
}
else
{
printf("文件不存在\n");
}
用w只写模式 打开并创建文件:
FILE* writeFile = fopen("E:\\textfile\\黑名单.txt", "wb");
if (writeFile)
{
printf("文件创建成功\n");
fclose(writeFile);
writeFile = NULL;
}
else
{
printf("创建失败\n");
}
给文件改名:已有名字,新的名字:
printf("改名字:%d\n", rename("E:\\textfile\\黑名单.txt", "E:\\textfile\\black.txt"));
删除文件:
remove("E:\\textfile\\black.txt");
文本文件-写操作
覆盖重写模式:
FILE* wf = fopen("E:\\textfile\\black.txt", "wt");
if (wf)
{
printf("a=%d name is %s %8.2f\n", 10, "张三", 3.14159);
putchar('A');
puts("hello world");
fprintf(wf, "a=%d name is %s %8.2f\n", 10, "张三", 3.14159);
fflush(wf);//清除文件缓冲区
fputc('A', wf);
fputs("hello world", wf);
fclose(wf);
wf = NULL;
}
打开追加模式:
FILE* af = fopen("E:\\textfile\\black.txt", "at");
if (af)
{
fprintf(af, "a=%d name is %s %8.2f\n", 10, "张三", 3.14159);
fputc('A', af);
fputs("hello world", af);
fclose(af);
af = NULL;
}
文本文件-读操作
读取若干个整数:
FILE* rf = fopen("E:\\textfile\\score.txt", "rt");
if (rf)
{
int num;
while (!feof(rf)&& fscanf(rf, "%d,", &num)>0) //end of file
{
printf("%d,\n", num);
}
fclose(rf);
rf = NULL;
}
读取若干行文字:
FILE* rf2 = fopen("E:\\textfile\\black.txt", "rt");
if (rf2)
{
char str[100];
/*while (!feof(rf2) && fscanf(rf2, "%s", str) > 0)
{
printf("%s\n", str);
}*/
while (!feof(rf2) )
{
fgets(str, 100, rf2);
printf("%s", str);
}
fclose(rf2);
rf2 = NULL;
}
二进制文件-写操作
打开文件流:
FILE * writeFile=fopen("E:\\textfile\\data.txt","wb");
这里的文件名后缀虽然是txt文本文件,但不能通过拓展名看文件本质。
存储一个整型变量:
int age = 5;
fwrite(&age, sizeof(int), 1, writeFile);
存储一个数组:
int arr[5] = { 65,66,67,90,97 };
fwrite(arr, sizeof(int), 5, writeFile);
fwrite(arr, sizeof(arr), 1, writeFile);
存储一个字符串:
char* ps = "hello world";
char* qq = "hello world";//常量区
ps = "how are you";
ps = "how old are you";
const int ch = 'M';
//char* p = &ch;
fwrite(&ch, sizeof(char), 4000, writeFile);
存储一个结构体:
typedef struct
{
char a;
double b;
int c;
} AAA;
AAA a = { 'A',3.14,100 };
fwrite(&a, sizeof(AAA), 1, writeFile);
关闭文件流:
fclose(writeFile);
writeFile = NULL;
二进制文件-读操作
打开文件流:
FILE * readFile=fopen("E:\\textfile\\data.txt","rb");
读取一个整型变量:
int a ;
while (fread(&a, sizeof(int), 1, readFile)>0)
{
printf("%d", a);
}
读取一个数组:
int arr[11];
rewind(readFile);
printf("读取的个数 %d\n", fread(arr, sizeof(int), 11, readFile));
读取一个结构体:
typedef struct
{
char a;
double b ;
int c;
} AAA;
AAA b;
AAA* p = (AAA*)malloc(sizeof(AAA));
fread(&b, sizeof(AAA), 1, readFile);
fseek(readFile, 0, SEEK_SET);
if (p)
{
fread(p, sizeof(AAA), 1, readFile);
}
free(p);
关闭文件流:
fclose(readFile);
readFile = NULL;
文件复制
用文件指针打开被复制文件和目标文件的缓冲区,再建立一个buffer数组充当中间缓冲区。
FILE * from = fopen("E:\\example.m4v","rb");
FILE * to = fopen("e:\\bmd.m4v","wb");
unsigned char buffer[1024];
将被复制文件的数据先一点一点保存到buffer数组中,再将buffer数组中的数据写入到目标文件,直至被复制文件中的光标到达尾部。
if (from && to)
{
while ((len = fread(buffer, sizeof(char), 1024, from)) > 0)
{
fwrite(buffer, sizeof(char), len, to);
}
fclose(from);
fclose(to);
}
文件光标
打开文件流:
FILE* rf = fopen("E:\\textfile\\letter.txt", "r+t");
通过ftell()读取光标位置,通过fgetc()读取一个字:
printf("%ld\n", ftell(rf));
putchar(fgetc(rf));
printf("%ld\n", ftell(rf));
putchar(fgetc(rf));
printf("%ld\n", ftell(rf));
修改:
修改前 需要用fseek()函数 重新定位
从当前位置偏移0,然后改为'8'。
fseek(rf, 0, SEEK_CUR);
fputc('8', rf);
SEEK_CUR 定位在当前位置
SEEK_END 定位在结尾
SEEK_SET 定位在开头
也可以用rewind() 使光标回到开头
rewind(rf);
printf("%ld\n", ftell(rf));