Thread: Sime I/O Question

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    18

    Sime I/O Question

    I am opening a file for reading/writing/whatever in C.

    How would I grab a certain line of text? Say if I wanted to grab all the text on the 3rd line of a .txt file, how would I do it?

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Read every line into a buffer and keep a count. When the count equals three (or two, depending on your scheme), then copy the buffer into the required variable.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For text files, you're right, you almost always have to read the file sequentially. The only 2 exceptions I can think of are:

    1) Make each line the exact same length. Then you could use fseek() to move line_length * 2 into the file and you'd be at the beginning of the 3rd line.

    2) Create some sort of index that knows the offset into the file for each line. Then you could use fseek() to move to that offset.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I get way too carried away with these things sometimes:
    Code:
    itsme@itsme:~/C$ cat nlindex.c
    // Creates an index file (nline.idx) that contains file offsets of
    // newlines that exist in the input file.
    //
    // Usage: nlindex <input file>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    void write_offset(FILE *fp, long offset)
    {
      fwrite(&offset, sizeof(offset), 1, fp);
    }
    
    int main(int argc, char **argv)
    {
      FILE *fpin, *fpout;
      int c;
    
      if(argc != 2)
      {
        fputs("Usage: nlindex <input file>", stderr);
        exit(EXIT_FAILURE);
      }
    
      if(!(fpin = fopen(argv[1], "r")))
      {
        fprintf(stderr,
          "Couldn't open '%s' for reading: %s\n", argv[1], strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      if(!(fpout = fopen("nline.idx", "wb")))
      {
        fprintf(stderr,
          "Couldn't open 'nline.idx' for writing: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      while((c = fgetc(fpin)) != EOF)
        if(c == '\n')
          write_offset(fpout, ftell(fpin) - 1);
    
      fclose(fpin);
      fclose(fpout);
    
      return EXIT_SUCCESS;
    }
    Code:
    itsme@itsme:~/C$ cat nlfirstchar.c
    // Prints the first character of every line in the input file.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int argc, char **argv)
    {
      FILE *fptxt, *fpidx;
      int c;
      long offset;
    
      if(argc != 2)
      {
        fputs("Usage: nlfirstchar <input file>\n", stderr);
        exit(EXIT_FAILURE);
      }
    
      if(!(fptxt = fopen(argv[1], "r")))
      {
        fprintf(stderr,
          "Couldn't open '%s' for reading: %s\n", argv[1], strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      if(!(fpidx = fopen("nline.idx", "rb")))
      {
        fprintf(stderr,
          "Couldn't open 'nline.idx' for reading: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      if((c = fgetc(fptxt)) != EOF)
        putchar(c);
      while(fread(&offset, sizeof(offset), 1, fpidx))
      {
        fseek(fptxt, offset + 1, SEEK_SET);
        if((c = fgetc(fptxt)) == EOF)
          break;
        putchar(c);
      }
    
      putchar('\n');
    
      fclose(fptxt);
      fclose(fpidx);
    
      return EXIT_SUCCESS;
    }
    Code:
    itsme@itsme:~/C$ cat forindex.txt
    it's too much work to do it this way.
    this is nuts.
    somewhere, this might serve a purpose.
    mildly entertaining anyway.
    everyone's a critic.
    800 llamas can fill a swimming pool.
    600 could probably do it.
    Code:
    itsme@itsme:~/C$ ./nlindex forindex.txt
    itsme@itsme:~/C$ ./nlfirstchar forindex.txt
    itsme86
    itsme@itsme:~/C$
    Clever, no?

    Anyway, this would make it easy to grab the 3rd offset from the index file, fseek() to that offset in the text file, and then use fgets() to read in that line.
    Last edited by itsme86; 06-27-2006 at 09:51 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM