C Program to Print the Contents of File in Reverse Order

This C Program reverses the contents of a file and print it.

Here is source code of the C Program to reverse the contents of a file and print it. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Reverse the Contents of a File and Print it
  3.  */
  4. #include <stdio.h>
  5. #include <errno.h>
  6.  
  7. long count_characters(FILE *);
  8.  
  9. void main(int argc, char * argv[])
  10. {
  11.     int i;
  12.     long cnt;
  13.     char ch, ch1;
  14.     FILE *fp1, *fp2;
  15.  
  16.     if (fp1 = fopen(argv[1], "r"))    
  17.     {
  18.         printf("The FILE has been opened...\n");
  19.         fp2 = fopen(argv[2], "w");
  20.         cnt = count_characters(fp1); // to count the total number of characters inside the source file
  21.         fseek(fp1, -1L, 2);     // makes the pointer fp1 to point at the last character of the file
  22.         printf("Number of characters to be copied %d\n", ftell(fp1));
  23.  
  24.         while (cnt)
  25.         {
  26.             ch = fgetc(fp1);
  27.             fputc(ch, fp2);
  28.             fseek(fp1, -2L, 1);     // shifts the pointer to the previous character
  29.             cnt--;
  30.         }
  31.         printf("\n**File copied successfully in reverse order**\n");
  32.     }
  33.     else
  34.     {
  35.         perror("Error occured\n");
  36.     }
  37.     fclose(fp1);
  38.     fclose(fp2);
  39. }
  40. // count the total number of characters in the file that *f points to
  41. long count_characters(FILE *f) 
  42. {
  43.     fseek(f, -1L, 2);
  44.     long last_pos = ftell(f); // returns the position of the last element of the file
  45.     last_pos++;
  46.     return last_pos;
  47. }

$ gcc file12.c
$ cat test2
The function STRERROR returns a pointer to an ERROR MSG STRING whose contents are implementation defined.
THE STRING is not MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function.
$ a.out test2 test_new
The FILE has been opened..
Number of characters to be copied 203
 
**File copied successfully in reverse order**
$ cat test_new
 
.noitcnuf RORRERTS eht ot llaC TNEUQESBUS a yb nettirwrevo ebyam dna ELBAIFIDOM ton si GNIRTS EHT
.denifed noitatnemelpmi era stnetnoc esohw GNIRTS GSM RORRE na ot retniop a snruter RORRERTS noitcnuf ehT
$ ./a.out test_new test_new_2
The FILE has been opened..
Number of characters to be copied 203
 
**File copied successfully in reverse order**
$ cat test_new_2
The function STRERROR returns a pointer to an ERROR MSG STRING whose contents are implementation defined.
THE STRING is not MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function.
$ cmp test test_new_2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.