Saturday, November 22, 2014

file reverse copy using pointers

# include <stdio.h>

main()
{
FILE*f1,*f2;
char*str;
int sz,ch;
f1=fopen("c:\\a.txt","r");
f2=fopen("c:\\b.txt","w");

fseek(f1,0,SEEK_END);
sz=ftell(f1);
str=(char*)malloc(sz);
*(str+sz)=NULL;

fseek(f1,0,SEEK_SET);
while((ch=getc(f1))!=-1)
{
*(str+ --sz)=ch;
}

fputs(str,f2);
fclose(f1);
fclose(f2);
}

file reverse command in c

/* in the following program read ing file from last and output char by char to another file */
/* file name : frev.c */
# include <stdio.h>

main(int argc,char*argv[])
{
FILE*f1,*f2;
int ch;
if(argc<3)
{
perror("unable to open... \n usage :  frev <src file>  <dest file>");
exit(0);
}

f1=fopen(argv[1],"r");
f2=fopen(argv[2],"w");

fseek(f1,0,SEEK_END);
while(fseek(f1,-2,SEEK_CUR)==0)
{
ch=getc(f1);
putc(ch,f2);
}
fclose(f1);
fclose(f2);
}