Thread: Search using strncmp and strlen?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    33

    Search using strncmp and strlen?

    So, my program has a search option to search from a file. But here, I'm using strcmp function so the strings has to match exactly with the compared string.
    My instructor told me to use strncmp and strlen to make the search option better. Although I don't know how to use it.
    Here is the code:
    Code:
    int search()
    {
         char target[50];
         int flag=0;
    
    
    
         struct item food;
    
          FILE *pfile;
    
        pfile = fopen("food menu.txt","rb");
    
       if(pfile != NULL)
       {
           printf("Enter Item name\n");
           scanf("%s",&target);
           fflush(stdin);
    
            do
             {
                 fread(&food,sizeof(food),1,pfile);
    
                 if(!feof(pfile));
                 {
        if(strcmp(target,food.item_name)==0)
        {
    
             printf("\n%-20s %18s %28.2f\n",food.item_name,food.item_type,food.item_price);
             flag=1;
             system("pause");
             return SUCCESS;
    
        }
                 }
    
        }while(!feof(pfile));
        }fclose(pfile);
    
      if(flag==0)
         {
             printf("NO MATCH FOUND");
         }
    
    
    }
    Any help would be appreciated. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not use strstr? In fact, I think that this has been suggested to you in a previous thread (if I did not mistake you for another member).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    ^I didn't posted any thread like this. So this must be a misunderstanding. But I'll look up strstr. I've never heard of it though.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, my apologies for the mistaken identity.

    By the way, to read a string into an array named target with scanf, you would pass target, not &target, as the argument. Also, to avoid buffer overflow, you would use "%49s", not just "%s", since the string can have a length of at most 49 (remember there must be space for the null character).

    Oh, and note that fflush(stdin) results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Ah...I see. Thanks. My program is working though, but I'll apply these changes.

    But my real question is how do I use strstr, strncmp or ctelen in that function.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by philia
    But my real question is how do I use strstr, strncmp or ctelen in that function.
    You need to read up on the relevant documentation first, e.g., if you are on a *nix system, [b]man strstr[/man] will show you what are the parameters and return type of strstr and what does it do, and from there you can decide how you might use it: though you need to be clear on what you are trying to do. You could also search the Web.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    strlen function basically computes the of a string, and in your case you'd use this function on the word that the user is looking for....(i.e. int iLen = strlen(target); ). The strncmp function is almost the same as the strcmp function your already using except it has an additional parameter; int size. So, basically you'd use the resultant from the strlen() function (int iLen) as the additional parameter (int size) in the strncmp() function. i.e. strncmp (target, food.item_name, iLen).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Another poster that could do well to learn the benefit of indentation.
    Code:
    int search()
    {
      char target[50];
      int flag = 0;
    
      struct item food;
    
      FILE *pfile;
      pfile = fopen("food menu.txt", "rb");
    
      if (pfile != NULL) {
        printf("Enter Item name\n");
        scanf("%s", &target);
        fflush(stdin);
    
        do {
          fread(&food, sizeof(food), 1, pfile);
    
          if (!feof(pfile));
          {
            if (strcmp(target, food.item_name) == 0) {
    
              printf("\n%-20s %18s %28.2f\n", food.item_name, food.item_type,
                     food.item_price);
              flag = 1;
              system("pause");
              return SUCCESS;
            }
          }
        } while (!feof(pfile));
      }
      fclose(pfile);
    
      if (flag == 0) {
        printf("NO MATCH FOUND");
      }
    }
    A couple of points
    1. if (!feof(pfile)); does nothing, because of the ; at the end.
    2. You return SUCCESS without ever closing the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would test return value of fread instead of feof flag for indication of the read success.
    Code:
    while(1 == fread(&food, sizeof(food), 1, pfile))
    {
            if (strcmp(target, food.item_name) == 0) {
     
              /* do your stuff */
            }
    }
    PS. fflush(stdin) is undefined. This function is used for outgoing streams to force the current content of the stream buffer to appear on the screen or be written into the file. (without waiting for end-of line to appear in the stream for line buffered streams, for example)
    Last edited by vart; 04-14-2015 at 11:24 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about strncmp
    By sweetorangepie in forum C Programming
    Replies: 8
    Last Post: 03-24-2008, 01:15 PM
  2. strncmp
    By TL62 in forum C Programming
    Replies: 9
    Last Post: 01-16-2008, 05:12 PM
  3. strncmp like function in C#
    By x77 in forum C# Programming
    Replies: 4
    Last Post: 11-18-2007, 06:29 AM
  4. Help with strncmp fxn
    By 3kgt in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2003, 02:40 PM
  5. strncmp()
    By xlordt in forum C Programming
    Replies: 2
    Last Post: 10-23-2002, 01:54 PM