Skip to main content
Write a program to write contents of one file in reverse into another file.
CODING:
#include<stdio.h>
#include<errno.h>

long count_characters(FILE *);
void main()
{
int i;
long cnt;
char ch,ch1;
FILE *fp1,*fp2;


if(fp1=fopen("simple.txt","r"))
{
  printf("THE FILE HAS BEEN OPENED.\n");

  fp2=fopen("revire.txt","w");

  cnt=count_characters(fp1);

  fseek(fp1,-1L,2);

  printf("NUmber of Characters To be copied %d\n",ftell(fp1));

  while(ch)
  {
  ch=fgetc(fp1);
  fputc(ch,fp2);
  fseek(fp1,-2L,1);
  cnt--;
  }
  printf("\n--FILE COPIED SUCCESSFULLY IN REVERSE.--\n");
  }
  else
  {
   perror("Error Occured\n");
  }
  fclose(fp1);
  fclose(fp2);

  }

long count_characters(FILE *f)
  {
  fseek(f,-1L,2);
  long last_pos=ftell(f);
  last_pos++;
  return last_pos;

  }

Comments