In: Computer Science
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
C PROGRAM
#include<stdio.h>
// implement command line arguments
int main(int argc,char *argv[])
{
FILE *fp1,*fp2; // create two file pointers
int i=0,cnt; // declare integer variable i and
cnt
if(argc<3) // check arguments
{
// display this message and
terminate
printf("Error: Invalid
Arguments");
return -1;
}
fp1=fopen(argv[1],"r"); // open file of first argument
in read mode
fp2=fopen(argv[2],"w"); // open file of second
argument in write mode
if(fp1==NULL) // check the read file is not
correct
{
// then, display this message and
terminate
printf("\n%s File can not be
opend..\n",argv[1]);
return -1;
}
fseek(fp1,0,SEEK_END); // set file pointer at end of
file
cnt=ftell(fp1); // count the size of file
while(i<cnt) // create while loop until size of
file
{
i++; // increment by 1 of i
fseek(fp1,-i,SEEK_END); // set file
pointer 0 to end
fprintf(fp2,"%c",fgetc(fp1)); //
write each character by character into file pointer fp2
}
// close all files
close(fp1);
close(fp2);
return 0;
}
OUTPUT
D:\>reverse filein.txt fileOut
D:\>type fileOut
elif gnitset a si sihT
PROGRAM SCREENSHOTS***PLEASE DON'T FORGET TO GIVE THUMBS UP