Write a C program that reverses the bytes of a given arbitra
Write a C program that reverses the bytes of a given arbitrary length file and outputs them to another specified file, which should be overwritten if it already exists. So, invoking your program as follows ./reverse infile outfile will result in the first byte of outfile being the last byte of infile, and so on until the last byte of outfile is the first byte of infile. The given filenames are just an example, make your own infile. You must use fseek()/fread()/fwrite().
Solution
ans:
#include<stdio.h>
 #include<stdlib.h>
 int main()
 {
 FILE *s;//input file(Source file)
 FILE *d; //output file(Destination file i.e in reverse order)
 char ch;//to read charcter by character
 int i; //loop counter
 long int n;//represents number of characters in source file
 s=fopen(\"file1.c\",\"r\"); //opens source file in read mode
 d=fopen(\"file2.c\",\"w\");//opens destination file in write mode
 if(s==NULL || d==NULL)
 {
 printf(\"file is not opened\");
 exit(0);
 }
fseek(s,-1L,2);
 n=ftell(s); //gives the position of file pointer
 n++;
 fseek(s,-1L,2);//keeps cursor at last character of the file
 while(n){ //loop to read characters for source file
 ch=fgetc(s);//used to read one character from source file
 fputc(ch,d); //to insert into destination file
 if(ch==\'\ \') //if charater is \'\ \'i.e next line
 fseek(s,-3L,1);
 else
 fseek(s,-2L,1);
 n--;
 }
 fclose(s);
 fclose(d);
 return 0;
 }
Output:
file1.c
welcome to siit
 venky
 ajay
file2.c
yaja
 yknev
 tiis ot emoclewÿa

