Reverse content of a file



Consider the following example to reverse the content of a file:

CODE:

# include <stdio. h>

# include <errno. h>

long countingcharacters (FILE *);

void main ()

{

int j;

long count;

char ch1, ch2;

FILE *f1, *f2;

if (f1 = fopen (“File1. txt”, “r“))

{

printf (“The FILE is opened…\n”);

f2 = fopen (“File2. txt”, “w”);

count = countingcharacters (f1);

fseek (f1, -1L, 2);

printf (“Characters to copy %d\n”, ftell (f1));

while (count)

{

ch1 = fgetc (f1);

fputc (ch1, f2);

fseek (f1, -2L, 1);

count –;

}

printf (“\nCopied the file in reverse order\n”);

}

else

{

perror (“Error!!!\n”);

}

fclose (f1);

fclose (f2);

}

long countingcharacters (FILE *f3)

{

fseek (f3, -1L, 2);

long lastpos = ftell (f3);

lastpos ++;

return lastpos;

}