文件操作,对FILE 结构直接进行赋值的问题
测试<stdlib.h>快速排序和自己写的堆排序的速度,到网上找了 500 个随机数,但排序太快以致比不出来,又懒得继续找,就直接在存随机数的文件里面复制粘贴,500 -> 1000 -> 2000 -> 4000 -> 8000 ...... 到 10万 左右的时候,Notepad 基本上打不开了,就想着直接写程序来复制粘贴。
程序代码:
#include<stdio.h>
int main(void)
{
FILE * f = fopen("1.txt","a+");
FILE * new = NULL;
FILE the = *f;
new = &the;
rewind(new);
.........
fclose(f);
return 0;
这里用文件指针 f 打开了文本文件,然后把 f 指向的文件结构赋值给了自己声明的 the,再用指针 new 指向 the, 对 new 进行 rewind 操作时,程序中断。
gdb报错:0x00007ffafdc0a458 in ntdll!RtlRaiseStatus ()
哪位知道这样做不行的原因?
正确的做法是:
程序代码:
#include<stdio.h>
int main(void)
{
FILE * the = fopen("1.txt","r");
FILE * new = fopen("1.txt","a");
char ch;
while((ch = fgetc(the))!= EOF)
fputc(ch, new);
fclose(the);
fclose(new);
return 0;
}
以及,为什么同一时间,可以对一个文件进行两次 fopen 操作




