search file for values in lookup table

This is a discussion on search file for values in lookup table within the C Programming forums, part of the General Programming Boards category; I have a .c file and I have a lookup file. The lookup file is like follows: ¬int ¬short ¬long ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    Question search file for values in lookup table

    I have a .c file and I have a lookup file.

    The lookup file is like follows:

    ¬int
    ¬short
    ¬long
    ¬double
    ¬float
    ¬char

    in a .txt file.

    I want to search the .c file for the above strings by going through the lookup table, reading in each string and then searching for the current string in the .c file. And each time one is found, a counter is incremented.
    Any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would open the lookup file, read in a line, and then search for that string in the .c file. Repeat with all the lines in the lookup file.

    Seriously: what part are you asking a question about, and what is that question?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    This is what I have so far:

    Code:
    int varCount(FILE *a_file)
    {
        FILE *lookup=NULL;
        lookup=fopen("lookup.txt","r");
        char curr_var[10];
        
        curr_var=fgets(curr_var, 10, lookup);
    }
    i get an error with the fgets() functions! (Incompatible types in assignment)
    Any idea why?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    And care to share - what is the error?
    Except that you are trying to assign to the array?

    Code:
    while(fgets(curr_var, 10, lookup))
    {
       /* use your curr_var here */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    Error fixed.

    How do I search for the string in the c file?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to read a line (or part of a line) into a string (char array) and use one of the string functions to see if the string you want to find is in the string you read in.

    For exampe strstr() may be the function you are looking for to find a string in another string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    This is what I have now:
    Code:
    int varCount(FILE *a_file)
    {
        FILE *lookup=NULL;
        lookup=fopen("lookup.txt","r");
        char curr_var[10];
        char c[10];
        char *pch;
        int var=0;
        
        while(fgets(curr_var, 10, lookup)) //current value from lookup
        {
             while(!feof(a_file)) { 
             fscanf(a_file, "%s", c); //get string from file
             pch = strstr(c,curr_val); //look for curr_val in c
             if(pch==curr_val){        //if curr_val in string then increment val
             var++;}
                                  }
        }
    
        return(var);
    }
    but I get an error message at "pch = strstr(c,curr_val);" saying "curr_val undeclared (first use in this function)

    What have I done wrong?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have used a variable that hasn't been declared, just like the compiler says. In this case, I think it's because of a typo, you mean't curr_var, not curr_val.

    Also, perhaps you should read in the "strings to search for" once and store in an array, instead of reading the file over and over again [which you currently don't do right anyways]. (Actually, checking the code again, you should be reading the source file many times over, as the current code looks, which is an even worse solution, as a 10K source file would then cause 60K of data reading).

    Finally, your current code will fail if for this code:
    Code:
    int someQuiteLongIdentifierName;
    because the "someQuiteLongIdentifierName" is (much) longer than the variabl "c" that should hold it.

    edit: and before Elysia says it: Your indentation kind of sucks.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    All I want to do is search the c file for int, char etc... which are listed in the lookup file and therefore count how many variables are in the c file.
    I am reading the c file 10 chars at a time, taking this 10 chars and looking for the current 'int' or 'short' or whatever in that 10 chars.
    But i dont know how to search a string for a substring...

  10. #10
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by sixstringsgaret View Post
    All I want to do is search the c file for int, char etc... which are listed in the lookup file and therefore count how many variables are in the c file.
    I am reading the c file 10 chars at a time, taking this 10 chars and looking for the current 'int' or 'short' or whatever in that 10 chars.
    But i dont know how to search a string for a substring...
    And if i give you this as input:
    Code:
    #include <stdio.h>
    int x;
    You read '#include <' no int here, next input.
    You read 'stdio.h>
    i' <- Yes including the \n but no int here, next input
    You read 'nt x;[EOF]' still no int here.

    So you must reconsider your thinking about reading 10 chars from the file.
    There is simple sample code at c library reference at the stdio routines that reads a whole file into memory.
    Try searching for that code, you might pick up something on the way too, and adopting it to your program.
    Last edited by xuftugulus; 03-04-2008 at 03:14 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I understand what you want to do, and your search in itself works fine. The problem is that you probably shouldn't attempt to read 10 chars at a time - that is bound to fail. Read a "token" at a time, but that may be much longer than 10 chars.

    If you insist on reading 10 chars at a time, consider this line:
    Code:
             1         2
    12345678901234567890123456789
    int one; int some_other;
    So, you read 10 chars:
    Code:
    int one; i
    Next you read another bit:
    Code:
    nt some_ot
    Now you missed the "int" that was in between.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    I forgot to say that i intend going through the file by 1 position each time
    eg:
    int number; int another_number;

    first: int number //first occurance
    second: nt number;
    third: t number;
    fourth: number; i
    fifth: number; in
    sixth: umber; int //second occurance

    see where i'm going?

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Ok, and why not load the whole file into a buffer, and keep doing strstr until you see you must read past the entire buffer? It will be both more elegant, more efficient and easier to track bugs in.

    Oh and if you see the declaration of strstr it says something about haystack and needle for it's string parameters. Give the poor function a haystack
    Last edited by xuftugulus; 03-04-2008 at 03:25 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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