fgeting through a file to find a certain line

This is a discussion on fgeting through a file to find a certain line within the C Programming forums, part of the General Programming Boards category; The file: Code: id:name:type:ssid:secret --------------------- 00:Temp1:1:Terminus:1sdfsdfdsffdfdfffssffdsfssfsdfdsfdsffdsfd86364aefe6f5f57705c4dd36e 01:Temp2:1:Something:sdkfjr8sjre7sdjfc42755sdfsdfc367c8e68sd8fsdfdshjsd8878dfjd87dsjd The code: Code: char line[128]; while (fgets(line, sizeof line, fp) != NULL && ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    fgeting through a file to find a certain line

    The file:
    Code:
    id:name:type:ssid:secret
    ---------------------
    00:Temp1:1:Terminus:1sdfsdfdsffdfdfffssffdsfssfsdfdsfdsffdsfd86364aefe6f5f57705c4dd36e
    01:Temp2:1:Something:sdkfjr8sjre7sdjfc42755sdfsdfc367c8e68sd8fsdfdshjsd8878dfjd87dsjd
    The code:
    Code:
    char line[128];
    while (fgets(line, sizeof line, fp) != NULL && (strcmp(strndup(line, 2), "00") == 0))
         printf("%s", line);
    }
    Yelds nothing.

    However, this code:
    Code:
    while (fgets(line, sizeof line, fp) != NULL && (strcmp(strndup(line, 2), "id") == 0))
    Yelds the line: id:name:type:ssid:secret

    The way a read the while-line is:
    - If the line is NOT NULL (i.e., there IS actually a line), AND the first two characters in this same line equals "00" (or "id"), then print the line.

    But how come it "works" when searching for the two first characters in the first line, but not when searching for the two first characters in the other lines? (EDIT: It doesn't work, really, because if I copy the first line to the end of the file, it still only finds the first.)

    Can someone please explain why I'm seeing this all wrong? And perhaps tell me how to write the while-sentence so that it gets the lines while they're not ZERO AND the first two characters equals (f.ex.) "02"? I.e.: Search for one or more lines starting with two specific characters. Or is it better to just check the lines inside the while-loop and break; out of it if there's a match? Like this:
    Code:
    while (fgets(line, sizeof line, fp) != NULL) {
         if (strcmp(strndup(line, 2), "00") == 0) {
              printf("%s\n", line);
              break;			
         }
    }
    Last edited by cnewbie1; 05-28-2011 at 03:09 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since the first line fails the match, the while loop is done and it's game over.

    (ETA: Also, you're going to make a zillion memory allocations that you can't release with this approach. Just use line[0]== and line[1]==.)
    Last edited by tabstop; 05-28-2011 at 03:18 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Since the first line fails the match, the while loop is done and it's game over.

    (ETA: Also, you're going to make a zillion memory allocations that you can't release with this approach. Just use line[0]== and line[1]==.)
    Or strncmp() if his library supports it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  2. Can't Find this! Dotted line with scissors
    By Stan100 in forum Tech Board
    Replies: 4
    Last Post: 10-17-2005, 09:24 PM
  3. Help! I cant find line numbers
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2005, 08:02 AM
  4. cant find where my error is in the line counter
    By Led Zeppelin in forum C Programming
    Replies: 5
    Last Post: 03-23-2002, 04:06 PM
  5. Equation to find endpoints on a line
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-11-2001, 01:45 AM

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