1
0

reverse.c 885 B

12345678910111213141516171819202122232425262728293031323334
  1. /* reverse.c -- displays a file in reverse order */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define CNTL_Z '\032' /* eof marker in DOS text files */
  5. #define SLEN 81
  6. int main(void)
  7. {
  8. char file[SLEN];
  9. char ch;
  10. FILE *fp;
  11. long count, last;
  12. puts("Enter the name of the file to be processed:");
  13. scanf("%80s", file);
  14. if ((fp = fopen(file,"rb")) == NULL)
  15. { /* read-only mode */
  16. printf("reverse can't open %s\n", file);
  17. exit(EXIT_FAILURE);
  18. }
  19. fseek(fp, 0L, SEEK_END); /* go to end of file */
  20. last = ftell(fp);
  21. for (count = 1L; count <= last; count++)
  22. {
  23. fseek(fp, -count, SEEK_END); /* go backward */
  24. ch = getc(fp);
  25. if (ch != CNTL_Z && ch != '\r') /* MS-DOS files */
  26. putchar(ch);
  27. }
  28. putchar('\n');
  29. fclose(fp);
  30. return 0;
  31. }