早教吧 育儿知识 作业答案 考试题库 百科 知识分享

究竟错在哪了?#includemain(){\x05FILE*p,*q;\x05inti,c[10]={1,2,3,4,5,6,7,8,9,10};\x05p=fopen("1.c","wb");\x05for(i=0;i=0)\x05{\x05\x05fputc(fgetc(p),q);\x05\x05fseek(p,-2,1);\x05}fclose(p);fclose(q);q=fopen("2.c","rb");\x05rewind(q);\x05for(i=

题目详情
究竟错在哪了?
#include
main()
{
\x05FILE *p,*q;
\x05int i,c[10]={1,2,3,4,5,6,7,8,9,10};
\x05p=fopen("1.c","wb");
\x05for(i=0;i=0)
\x05{
\x05\x05fputc(fgetc(p),q);
\x05\x05fseek(p,-2,1);
\x05}
fclose(p);
fclose(q);
q=fopen("2.c","rb");
\x05rewind(q);
\x05for(i=0;i
▼优质解答
答案和解析
问题出在
q=fopen("2.c","wb");
fseek(p,0,2);
fseek(p,-1,1);
while(ftell(p)>=0)
这几句.
fseek会把文件结束符清除掉,请看linux man page里面的一句话:
A successful call to the fseek() function clears the end-of-file indicator for
the stream and undoes any effects of the ungetc(3) function on the same stream.
所以你的while会无限循环了.