Thread: How to compare a string with a set of string that have been stored in a file

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    30

    Question How to compare a string with a set of string that have been stored in a file

    I am trying to compare a string that i have entered with a set of strings that have already been stored in a file. I am using strcmp function but i am not getting the result. kindly help
    Code:
    printf("\nEnter string:");
    scanf("%s",&m);
    
    
    ptr_file =fopen("abc.text","r");
    
    
    fscanf(ptr_file,"%s",buf);
        
        h=strcmp(buf, m);
        
        if (h==0)
           
          printf("\n matched");
        else
        printf("\n Not matched");
    Last edited by shan2014; 05-05-2014 at 06:40 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First of all, you should be checking that your file has opened successfully before trying to access it. I suspect this might be the problem - you have the extension as ".text" but I would think you mean the more typical ".txt".

    Code:
    scanf("%s",&m);
    If 'm' is a buffer, you don't need the '&' there.

    If working with strings, I recommend using "fgets()" both for user input (stdin) and reading from the file.

    Finally, if you're checking many strings in the file, you would need a loop in your code - but maybe this just hasn't been implemented yet.

  3. #3
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    I would advice that you start small. You should always break down what you want to do in smaller an hopefully simpler parts.

    So first off, write a program that reads your file from disk and prints every string in it. Post something along those lines first and we'll go from there.

    A few hints to get you going. Check the return value from fopen(3) so you are sure that you actually could open the file. I'd advice to use fgets(3) instead of fscanf(3) to read your strings from the file. You will have to use a loop togheter with fgets to read all the strings from the file.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by Matticus View Post
    First of all, you should be checking that your file has opened successfully before trying to access it. I suspect this might be the problem - you have the extension as ".text" but I would think you mean the more typical ".txt".

    Code:
    scanf("%s",&m);

    If 'm' is a buffer, you don't need the '&' there.

    If working with strings, I recommend using "fgets()" both for user input (stdin) and reading from the file.

    Finally, if you're checking many strings in the file, you would need a loop in your code - but maybe this just hasn't been implemented yet.
    My file has .text as the extension and hence i am using that. m is a char type declared as an array to store the new string want to compare with the strings stored in the file.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by Jimmy View Post
    I would advice that you start small. You should always break down what you want to do in smaller an hopefully simpler parts.

    So first off, write a program that reads your file from disk and prints every string in it. Post something along those lines first and we'll go from there.

    A few hints to get you going. Check the return value from fopen(3) so you are sure that you actually could open the file. I'd advice to use fgets(3) instead of fscanf(3) to read your strings from the file. You will have to use a loop togheter with fgets to read all the strings from the file.
    this is the code that i am using to read the strings from the file...
    Code:
    FILE *ptr_file;
    
    char buf[50],m[50];
         
       ptr_file =fopen("abc.text","r");
        
        if (!ptr_file) 
           
           printf("Error opening file");
            
        else
        {
    
         while (fgets(buf,50, ptr_file)!=NULL)
    
    printf("%s",buf);
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    My file has .text as the extension and hence i am using that.
    Fair enough - you should still be checking that the file has successfully opened before accessing it, though.

    m is a char type declared as an array to store the new string want to compare with the strings stored in the file.
    Indeed - the suggestion I offered assumed this.

    --------

    In response to your next post - this is quite different than what you originally posted.

    Try this:

    - Create and post a simple, yet complete, code example that compiles (with your compare included)
    - Post what is contained in a sample ".text" file
    - Explain exactly what you are entering, and the result you are getting

  7. #7
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by shan2014 View Post
    m is a char type declared as an array to store the new string want to compare with the strings stored in the file.
    So, m is some kind of buffer then. Now go and reread what Matticus posted for you.

    And FYI, it is much easier to help you if you post code that is complete, so we don't have to sit around guessing what you have done. Also, make sure that the code you post compiles _without_ warnings. Also, it is usually a smart move to post an example of how your text file looks, even if we probably can make an educated guess. It makes it more likely that you will get help and that you will get help faster.

    EDIT: Seeing as you are one step ahead of me Matticus, I'll leave this one to you! :-)

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by Matticus View Post
    Fair enough - you should still be checking that the file has successfully opened before accessing it, though.



    Indeed - the suggestion I offered assumed this.

    --------

    In response to your next post - this is quite different than what you originally posted.

    Try this:

    - Create and post a simple, yet complete, code example that compiles (with your compare included)
    - Post what is contained in a sample ".text" file
    - Explain exactly what you are entering, and the result you are getting

    well here this the full code
    Code:
    FILE *ptr_file;
    
    char buf[50],m[50];
    
       ptr_file =fopen("abc.text","r");
    
        if (!ptr_file) 
    
           printf("Error opening file");
    
        else
        {
    
         while (fgets(buf,50, ptr_file)!=NULL)
    
    printf("%s",buf);
    }
    printf("\nEnter string:");
    scanf("%s",&m);
        ptr_file =fopen("abc.text","r");
            fscanf(ptr_file,"%s",buf);
        
        h=strcmp(buf, m);
        
        if (h==0)
         printf("\n matched");
            else
            printf("\n Not matched");
    
    
    my file 'abc' contains the following
    [email protected]
    [email protected]
    [email protected]
    So i enter various string to find if they are matched or not.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Turn on warnings! Do NOT ignore them.

    scanf("%s",&m);

    Why do you think &m is better than m by itself.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, that's not a complete compilable example (no includes, no "main", 'h' not declared, etc).

    Anyway, first you open the file, then read the contents with "fgets()" and print in a loop. So far, so good.

    Then ... you try to open the file again. That is a problem. Instead, just "rewind()" the file to start back from the beginning. You also need to restructure the code so this portion onward won't happen if the file failed to open. If the entire program hinges on the file being successfully read, then just exit on file read error (with appropriate message) - then the rest of the code doesn't have to be in an "else" block.

    You still haven't fixed the "scanf()" argument I mentioned in post #2. In fact, if you used "fgets()" to read/print all lines in the file, why do you change of "fscanf()" when you start looking for the string of interest?

    Also, you're not checking for the string of interest in a loop, so all you're doing is checking the first string in the file.

    And you need to close the file when you're done with it.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Also never use a function to retrieve a C-string that doesn't limit the number of characters it will retrieve. The scanf() series of functions can limit the number of characters it will retrieve by using the width specifier, just don't forget to save room for the end of string character.

    Code:
    char C_string[100];
    scanf("%99s", C_string);

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and compare file contain array of string
    By didinj in forum C Programming
    Replies: 6
    Last Post: 12-13-2012, 03:30 AM
  2. string compare
    By ingeniousreader in forum C Programming
    Replies: 7
    Last Post: 03-02-2012, 12:43 PM
  3. String Compare
    By shruthi in forum C Programming
    Replies: 2
    Last Post: 02-09-2012, 09:27 AM
  4. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  5. Replies: 8
    Last Post: 09-04-2006, 07:54 PM

Tags for this Thread