home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / cp1 / rfile.c < prev    next >
Text File  |  1993-06-10  |  2KB  |  69 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-06-93 (12:39)             Number: 85
  4. From: JOHN GREEP                   Refer#: 47
  5.   To: JEFFERY FOY                   Recvd: NO  
  6. Subj: Reverse sorting?               Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. JF>Say you have a text file like this:
  9.  
  10. JF>first
  11. JF>second
  12. JF>third
  13. JF>...
  14. JF>six-hundred-ninety-eight
  15. JF>six-hundred-ninety-nine
  16. JF>seven thousand
  17.  
  18. JF>How would you reverse the lines (i.e. in the example, seven thousand
  19. JF>would become the first line, six-hundred-ninety-nine would become the
  20. JF>second line ... first would become the last line).
  21.  
  22. This could be done by sorting the file in descending order.  If it's
  23. already sorted or in another desired order, you would have to read the
  24. file from the end and work toward the beginning...Read a record and
  25. write to a temporary file.  When it's done, delete the original, and
  26. rename the temporary file.  You've got it tough though if each line is
  27. a different length.
  28.         On a different thought, recursion would be perfect for this job.
  29.         This is just off the top of my head, so if there are any bugs,
  30.         you can let me know eh?
  31. .
  32. .
  33. .
  34. ___---------Cut-Here-----------8<-----------------------------
  35. /*
  36.    Recursive function to reverse a file.
  37.    Donated to the Public Domain by John Greep  June 6, 1993
  38.  
  39.    fp is a pointer to a file open for reading, positioned at the
  40.         beginning of the file.
  41.    tm is a pointer to a temporary file open for writing at the
  42.         beginning of the file.
  43.  
  44.    Returns:
  45.                 -1 on error or file empty.
  46.                  0 on success.
  47. */
  48. int reverse (FILE *fp, FILE *tm)
  49. {
  50.         char temp[81]
  51.         if(NULL != fgets (temp, 81, fp))
  52.                 return -1;
  53.         reverse (fp, tm);
  54.         fputs (temp,tm);
  55.         return 0;
  56. }
  57. ___---------Cut-Here-----------8<-----------------------------
  58.  
  59.         _____________
  60.         _/ () |-| /\/
  61.  
  62.  * OLX 2.1 TD * Press any key to continue or any other key to quit
  63.  
  64. --- Maximus 2.01wb
  65.  * Origin: SECRET C::The C Source*HST DS*(403)256-7019,Calgary,Ab (1:134/21)
  66. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  67. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  68. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  69.