#include<stdlib.h>
#include<errno.h>
#include<stdio.h>
int main()
{
char *c=NULL,*c1,ch;
c=(char*)malloc(10*sizeof(char));
c1=(char*)malloc(10*sizeof(char));
char arg1[100],arg2[100];
int i,count=0;
FILE *fp,*fq;
printf("Name of the file:");
scanf("%s",arg1);
fp=fopen(arg1,"w+");
if(fp==NULL)
{
perror("Failed to open file");
return errno;
}
printf("\t\t\t%s\n\n",arg1);
printf("\t\tInput the text into the file\n");
printf("\t\tPress Ctrl+d to the stop\n");
while((*c=getchar())!=EOF)
{
fwrite(c,1,sizeof(c),fp);
count++;
}
printf("\n\n");
fclose(fp);
fp=fopen(arg1,"w+");
printf("Name of the output file:");
scanf("%s",arg2);
printf("Reversing the contents of the file.......\n");
fq=fopen(arg2,"w+");
printf("\t\t%s\n\n",arg2);
for(i=1;i<=count;i++)
{
fseek(fp,-(i+1),SEEK_END)
fwrite(c1,1,sizeof(c1),fq);
}
printf("Done....Opening the file\n");
rewind(fq);
for(i=0;i<=count;i++)
{
ch=getc(fp);
putc(ch,stdout);
}
fclose(fp);
fclose(fq);
return 0;
}
我想扭转输入文件的内容,并希望显示反转的内容,但我没有得到它;我认为我犯了一个逻辑错误。没有开始所需的输出
你得到什么输出?你有什么试图解决这个问题?你怎么知道它不工作? – 2011-04-19 16:11:27
如果文件内容很小,则可能需要将所有内容加载到内存中,然后在那里将其反转。那个'fseek(...)'和'rewind()'对我来说看起来效率非常低。 – abeln 2011-04-19 16:14:02
我已经编译了我的linux上的程序,我无法得到反转内容的输出 – 2011-04-19 16:14:50