Thread: How to search through a file for a certain string?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    How to search through a file for a certain string?

    I need to write a program that counts up all the semicolons in a function within a file...
    I know the algorithm but i'm having trouble putting into code...

    What i'm trying to do right now is search through the file for the string "FUNCTION" (which is included in the file to designate the start of the function)

    Is this the proper way to do it? or should I use a strcmp function? I get one error from this and it says "too many characters in constant" at the if (str== START) line

    Also, if there is no "FUNCTION", how do I proceed to the next line of the file to check again?


    #include <stdio.h>
    #include <string.h>


    #define START 'FUNCTION'

    int main(void)
    {
    FILE *fp;
    char str[100], fileName[100];

    printf("Enter file name: ");
    gets(fileName);

    fp = fopen(fileName, "r");

    if (fp==NULL)
    {
    printf("%s can not be opened.\n", fileName);
    }

    fgets(str, sizeof(str), fp);

    if (str == START)
    {
    printf("There is a function.");
    }



    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're making strings, you need double quotes:

    "this is a string"

    Not single quotes:

    'a'

    Also, you need to compare strings with strcmp, not the equality operator. ==

    Oh, and use code tags.


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

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Use strcmp function to compare the strings.

    And use the fgets statement inside a while loop.So that you can traverse the entire file content.

    Try like this,

    Code:
    while(fgets(str,sizeof(str),fp))
    {
    if(!strcmp(str,START))
     printf("There is a function.");
    }
    And as well as give the string FUNCTION inside the double quotes.
    like,
    Code:
    #define START "FUNCTION"
    Last edited by kiruthika; 03-08-2010 at 09:42 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So "FUNCTION" is used in the input file as a "delimiter", which basically says "the following lines are the body of this function", right? If so, I imagine there is a delimiter, say, "END", to tell you "there is nothing else in this function".

    If so, read the file line by line, and if the line equals the start delimiter string "FUNCTION", then on all subsequent line reads, sum up the number of ";"s. You could use, say, strchr - C++ Reference to find a character inside a string. It depends on what you've been told, but if a line can contain many ";"s, then you have to repeatedly call this function to find the next occurrence. That link has a good example, which you could use without much modification, if there can be more than one ";" on a line. Otherwise, just read a line, check if it has a ";", and add one to your counter; otherwise read next line.

    Once your in "function mode", that is, you've already read a "FUNCTION", then you continue reading the lines and summing up the number of ";"s until you read the "END", or whatever, delimiter.

    As for the
    Code:
    if (str == START)
    line, you can't compare strings this easily: you must use something like strcmp - C++ Reference. Note, once you use "strcmp" you'll still have a similar problem. The line
    Code:
    #define START 'FUNCTION'
    tells the compiler to literally do a "search/replace", for the token START and replaces it exactly with 'FUNCTION'. However, you probably meant this to be a "string". So change the 's to "s.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    yeah that's exactly right...thanks a lot...

    also, i need to output the function name...I plan on finding "FUNCTION" and then going up two lines because that is where the name of the function is located...

    for example...

    int find_primes(______)
    {
    FUNCTION
    .
    .
    .
    END
    }

    would i just decrease str by 2 and output the name?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    It isn't that easy. If the lines are fixed-length, then it's "almost" that easy, but I doubt they are fixed-length.

    One solution might be to always store the 1st and 2nd last lines. So once you reach "FUNCTION, in your above example, then the 1st and 2nd last lines would have been saved already and have the values "{" and "int find_primes(______)", respectively. Once you get it working, and if interested, you could make it slightly more efficient by not worrying about the 1st and 2nd last lines while "inside" the function, as you know you don't expect there to be a function inside this function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM