Sime I/O Question

This is a discussion on Sime I/O Question within the C Programming forums, part of the General Programming Boards category; I am opening a file for reading/writing/whatever in C. How would I grab a certain line of text? Say if ...

  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,163
    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,163
    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, 04:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 11:48 PM
  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, 02:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21