home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3650 / getnth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  778 b   |  39 lines

  1. /*
  2.  * get_nth_string.c  Brian Rice  rice@dg-rtp.dg.com  1989
  3.  * This routine is part of the shop package.
  4.  * It takes advantage of the fact that the first sizeof(long) bytes
  5.  * in the file named by f are a long which gives the number of
  6.  * items in that file.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "shop.h"
  11.  
  12. char *get_nth_string(f,n,buff,l)
  13. FILE *f;
  14. long int n;
  15. char *buff; 
  16. int l; 
  17.     long int begin_loc, end_loc;
  18.     int s_length;
  19.  
  20.     (void) fseek(f,n*BYTE_COUNT_SIZE+ITEM_COUNT_SIZE,0);
  21.     (void) fread(&begin_loc,sizeof begin_loc,1,f);
  22.     (void) fread(&end_loc,sizeof end_loc,1,f);
  23.  
  24.     s_length = end_loc - begin_loc;
  25.  
  26.     if (s_length > l - 1)
  27.         s_length = l - 1;
  28.  
  29.     (void) fseek(f,begin_loc,0);
  30.  
  31.     (void) fread(buff,sizeof(char),s_length,f);
  32.  
  33.         buff[s_length] = '\0';
  34.  
  35.     return buff;
  36. }
  37.  
  38.