Thread: How to find specific line in a text file?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    How to find specific line in a text file?

    Hi all. So I have an assignment where I need to find a specific line in a text file with the word "circles". Using that information I must somehow find how many circles there are. (the text files says how many circles there are before the word circles. eg. "3 circles") Next, I have to skip a certain amount of lines after circles and do the same as above for "friends". However, with friends, I have to read in each line and parse the information and store it in the friends array. Here's my code for now and it is VERY rough right now. Help would be very very nice as I am almost completely lost.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <printFriends.h>
    void main(int argc, char **argv)
    {
      int i,size, wordnum;
      char max[256];
      char *fname, *word, *friends, *circles;
      FILE *file;
    
    
      //Check for command line arguments
      if (argc <= 1)
        {
          printf("Insufficient number of inputs\n");
          exit(1);
        }
    
    
      //Open the file and check if file exists
      fname = argv[1];
      file = fopen(fname,"r");
    
    
      if (file == NULL)
        {
          printf("Cannot open input file %s\n",fname);
          exit(2);
        }
    
    
      //Read in file line by line and search word
      while(fgets(max, sizeof(max), file) != NULL)
        {
    
    
          size = strlen(max);
          circles = strstr(max, "circles");//????
          word = strtok(max," \n");
    
    
          while(word != NULL)
    	  word = strtok(NULL," \n");
        }
    
    
      //Allocate array for number of friends
      friends = (char *)malloc(//something);
      fclose(file);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in line 39 your circles pointer will be NULL if no "circles" word is found - in this case you need just skip to the next line... for exaple

    Code:
    if(circles == NULL)
       continue;
    if it is not null - it points to the char 'c' in the line looking like
    "blablabla 3 circles"

    putting 0 in the previous char will give you line like "blablabla 3"
    Code:
    if(circles > max)
    {
       circles--;
       *circles = 0;
    
    }
    after that you can use something like strrchr to find first space from the end and strtol to convert string to number
    ...
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vart View Post
    putting 0 in the previous char will give you line like "blablabla 3"
    Code:
    if(circles > max)
    {
       circles--;
       *circles = 0;
    
    }
    Unless "circles" starts the line, in which case you'll under-run your array bounds.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    word = strtok(max," \n");
    while(word != NULL)
    word = strtok(NULL," \n");
    Words are not separated by end-of-line character but space - this character should be used in strtok().
    Try this inside while(fgets( ... loop
    Code:
    char *prev, *curr;
    prev = strtok(max, " ");
    while ((curr = strtok(NULL, " ")) != NULL)
    {
        if (strstr(curr, "circle") != NULL) // without 's' in case of "1 circle"
        {
            break;
        }
        else
        {
            prev = curr;
        }
    }
    if (curr != NULL)
    {
        printf("Found %s %s in this line\n", prev, curr);
    }
    else
    {
        printf("No circles found in this line\n");
    }
    Last edited by DRK; 01-18-2012 at 02:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Reading a specific line from a text file
    By acidrain in forum C Programming
    Replies: 3
    Last Post: 12-01-2009, 02:23 PM
  3. Find specific text.
    By mmarab in forum C Programming
    Replies: 2
    Last Post: 05-30-2008, 02:34 AM
  4. Read SPECIFIC line from text file
    By 13373ar5 in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 05:14 AM
  5. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM

Tags for this Thread