Thread: Searching text file

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    18

    Searching text file

    Hey guys, I am having some trouble reading a file for a specific string then getting the next two lines of numbers after it. The file would look something like this:

    Bill
    12345 67890
    64528 09342
    Jeff
    34872 58908
    17830 47674
    Moose
    12465 06854
    48736 40985

    Say I need to get the numbers after Jeff. How should I do that?
    I have already been able to locate the desired string "Jeff".

    Thank you for your time.

    InicDominus

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    think of this as looking line by line for a sequence of events. so in these cases you will implement what is called a 'state machine'. if you haven't heard that term don't worry. there are multiple ways to implement such a thing. here is how I like to do it for a simple case like this (pseudocode)
    Code:
    state = 0
    while(read a line of text == success)
        switch(state) {
        case 0:
           // looking for 'jeff'
           if line == Jeff
              state = 1;
           break;
        case 1:
           // read the first number
           read number1 from line
           state = 2;
           break;
        case 2:
          // read the second number
          read number2 from line
          state = 3;
          break; 
        default:
          // all done, do nothing
          break;
        }
    } // end loop or whatever
    instead of a 'switch' you can use 'if' statements instead. 'if state == 1 do first step, state =2; if state == 2 do second step. state = 3...'

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post the code you use to find "Jeff", and we can give you specific advise. Generally, you have a file pointer that has just passed the 'f' in "Jeff" (fgets takes you up to the newline, fscanf() depends on the format specifiers).

    That's why I ask you to post the code you're using to find "Jeff".

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Thank you for the fast reply. I will give that a shot.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Sorry about the delayed response. I don't have internet acces on my coding computer so it takes some running around to get here. This is what i use to find the word:

    Code:
    File *file;
    file = fopen("/home/user/Desktop/text.txt", "r");
    char line[256];
    int line_num = 1;
    char term[12] = "SATELLITE 1";
    
    if(file != NULL)
    {
            while(fgets(line, sizeof(line), file))
            {
                    if(strstr(line, term) != NULL)
                    {
                            write(output_file, line, sizeof(term));
                    }
                        ++line_num;
            }
    }
    Also, I should mention that the actual text I read from wil be thousands of lines long and the special string could be anywhere. The length of all the numbers after it will be the same though. I know this may be a sloppy way of reading the file. I've never used C before this summer.
    Thanks again

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    And I'd like to assign each number to a variable.
    ex. num1 = 34872
    num2 =58908
    num3 = 17830
    num4 = 47674

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No problem.

    So right after you have confirmed that the target string has been found, (right before or after this line of code: write(output_file, line, sizeof(term));

    you need to use code similar to this sort of pseudo code:
    Code:
       use fgets() inside the if statement confirming you have the target string
       use sscanf(line, "%d %d", &num1, &num2);
       right afterward, to get the first two numbers
    
       repeat the fgets() and sscanf() just like above
       to get num3 and num4
    
       process anything you need to do with num1 - num4
       before the end of the if statement
    Last edited by Adak; 09-17-2012 at 01:49 PM.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Thank you very much this is great!

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Well, it looked good and I'm sure it's fine. I am just still having trouble getting to the next two lines after finding the target string. Some debugging I did said that when I used the sscanf(line, "%d %d", &num1, &num2); the num variables were empty. I am assuming that is because I do not know how to tell it to go to the line after the target string?
    Sorry for my confusion.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's a bit tricky. If you try to strcmp(line, targetName), then you will have the problem of fgets() adding in the newline at the end of the line, like it always does if it has room for it.

    Which causes the strcmp() to always fail.

    Study this:

    Code:
       while(fgets(line, sizeof(line),fp)) {
          sscanf(line, "%s", name);
          
          if((strcmp(target, name))==0) {
             fgets(line, sizeof(line),fp);
             sscanf(line, "%d %d",&num1, &num2);
             fgets(line, sizeof(line), fp);
             sscanf(line, "%d %d",&num3, &num4);
        
             printf("%s's numbers are %d, %d, %d and %d\n",target,num1,num2,num3,num4);
          }
       }

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Okay I got this one working. I just replaced that if statement with one I already had [ if(strstr(line, term) != NULL)]
    Thank you so much for your time and help!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by InicDominus View Post
    Okay I got this one working. I just replaced that if statement with one I already had [ if(strstr(line, term) != NULL)]
    Thank you so much for your time and help!
    Quite welcome.

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Okay, so of course after that works I am told that the data contains doubles and hex numbers so I should just store the whole line as a string. From the code above, I am already at the right line in the file. I am just having trouble saving the whole line as a string. I know sscanf only read until whitespace but I cannot find anything that just gets or copies the whole line to a string.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by InicDominus View Post
    Okay, so of course after that works I am told that the data contains doubles and hex numbers so I should just store the whole line as a string. From the code above, I am already at the right line in the file. I am just having trouble saving the whole line as a string. I know sscanf only read until whitespace but I cannot find anything that just gets or copies the whole line to a string.
    If I wanted to get a whole string (a row) of text from a file -- ready for this? -- I'd use filegetstring, which we call:

    Code:
    fgets(myCharArrayName, sizeof(myCharArrayName), myfilePointerName);
    Pretty slick, eh?

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    18
    Very slick!
    I tried that but I must be missing something /:
    That is in the line after the target string?
    Would I do the same thing to get the second line of data?
    When I try, it doesnt seem to save anything
    Last edited by InicDominus; 09-26-2012 at 10:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with searching text file
    By zacharyrs in forum C Programming
    Replies: 30
    Last Post: 12-01-2009, 03:13 PM
  2. Please Help in searching value in text file
    By david-_-zhang in forum C Programming
    Replies: 1
    Last Post: 02-10-2009, 03:10 AM
  3. searching text in a file
    By spveer in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 04:21 AM
  4. Searching a VERY large text file
    By Tankndozer in forum C Programming
    Replies: 4
    Last Post: 07-29-2004, 02:45 AM
  5. Searching A Text File?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 04:57 AM

Tags for this Thread