home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / FIGETS.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  1KB  |  65 lines

  1. /*
  2. ** figets like fgets only works backward from filepos pos instead of
  3. ** forward from the current filepointer
  4. **
  5. ** returns fileposition of the begin of line read
  6. **
  7. ** by Jan Vroonhof
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define MAXLEN 90
  14.  
  15. long figets(FILE *file, char *buffer, long pos)
  16. {
  17.       char *ptr;
  18.       long aap;
  19.  
  20.       aap = (pos - MAXLEN > 0 ? pos-MAXLEN : 0L);
  21.       fseek(file, aap, SEEK_SET);
  22.       fread(buffer + 100, 1, MAXLEN, file);
  23.       buffer[pos - aap + 100] = 0;
  24.       ptr = strrchr(buffer + 100, '\n');
  25.       if (ptr)
  26.       {
  27.             *ptr = 0;
  28.             ptr = strrchr(buffer + 100, '\n');
  29.             if (ptr)
  30.             {
  31.                   strcpy(buffer, ptr + 1);
  32.                   return aap + (ptr - buffer - 100) + 1;
  33.             }
  34.             else
  35.             {
  36.                   strcpy(buffer, buffer + 100);
  37.                   return (aap ? -1L : 0L);
  38.             }
  39.       }
  40.       else
  41.       {
  42.             strcpy(buffer, buffer + 100);
  43.             return -1L;
  44.       }
  45. }
  46.  
  47. #ifdef TEST
  48.  
  49. void main(void)
  50. {
  51.       char buf[256];
  52.       FILE *fp;
  53.       long pos;
  54.  
  55.       fp = fopen("figets.c", "r");
  56.       fseek(fp, 0L, SEEK_END);
  57.       pos = ftell(fp);
  58.       do {
  59.             pos = figets(fp, buf, pos);
  60.             puts (buf);
  61.       } while (pos);
  62. }
  63.  
  64. #endif /* TEST */
  65.