Thread: How do you move around a text file in C?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    How do you move around a text file in C?

    Hello! I'm new to C and i'm trying to figure out how to move down a text file and print out only certain lines.

    I know that fgets reads one line from a file but how do you skip 4 lines and then print out the 5th line?

    Do i use a while loop?

    eg.
    Code:
     
    FILE *ptr;
    int a = 0;
    char word[20];
    
    if ((ptr = fopen("input.txt","r") == NULL)
    {
    printf("Cannot open file");
    }
    else
    {
        while (a < 5)
        {
         ....don't know what to put here to skip these lines....
        }
        if (fgets(word,20,ptr) != NULL)   ......printing the 5th line
        {
        printf("&#37;s", word);
        }
    }
    fclose(ptr);
    i would just like to know what allows you to skip lines in a text file.
    Last edited by buggerboy_3; 04-29-2007 at 12:42 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you do not know the length of the line - you read it with fgets but do not print it - to skip.
    If you know the length - you can use fsetpos for example to skip known number of bytes
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    If you know the length - you can use fsetpos for example to skip known number of bytes
    Nope, you can't do arbitrary seeking on a text file. It has to be opened as a binary file.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Noir View Post
    Nope, you can't do arbitrary seeking on a text file. It has to be opened as a binary file.
    Yeah, right... And of course you have a quote from the reference guide to support your opinion?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    And of course you have a quote from the reference guide to support your opinion?
    Why yes, since you asked so nicely. Here's the quote that says fsetpos() won't work because you have to have been to the position first and called fgetpos():
    The fsetpos function sets the mbstate_t object (if any) and file position indicator
    for the stream pointed to by stream according to the value of the object pointed to by
    pos, which shall be a value obtained from an earlier successful call to the fgetpos
    function
    on a stream associated with the same file.
    And here's the quote that says you can't do arbitrary seeking on a text file with the only other option, fseek():
    For a text stream, either offset shall be zero, or offset shall be a value returned by
    an earlier successful call to the ftell function on a stream associated with the same file
    and whence shall be SEEK_SET.
    Oh, and those quotes are from the C standard, not just any old reference guide that happens to agree with me.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yeah, but this is about these 2 functions... but OMG we also have a fseek... What a suprise...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    yeah, but this is about these 2 functions...
    They won't work though. You can't jump to somewhere you haven't been yet and fsetpos() requires a successful call to fgetpos(). The only way to jump somewhere you haven't been is with fseek() and fseek() doesn't work for arbitrary jumps on a text file.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Noir View Post
    fseek() doesn't work for arbitrary jumps on a text file.
    No, it doesn't work on stream which are opened in text mode. Not the same thing. You can open a text file in binary mode, or a binary file in text mode, if you want.

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    That was bad wording on my part. I meant "text stream". Sorry for the confusion.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by buggerboy_3 View Post
    Hello! I'm new to C and i'm trying to figure out how to move down a text file and print out only certain lines.

    I know that fgets reads one line from a file but how do you skip 4 lines and then print out the 5th line?
    You don't "skip" lines in a text file, you read them in, BUT YOU ONLY MAY PRINT EVERY 4th OR 5TH LINE (whatever you want).

    You can use a while loop, like this:
    Code:
    while (there is another line to be read)  
       increment line_count;
       if ((line_count &#37; 4 == 0) && (line_count > 0))  /* selects every 4th line */
          print that line;
    loop
    So you're not "jumping over", your program is just selecting every 4th line to do something with. The other lines are just ignored.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM