Problem with Search function

This is a discussion on Problem with Search function within the C Programming forums, part of the General Programming Boards category; Hi, I can't seem to be able to figure out why this code: Code: char c[100]; char buf[32]; ... if ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    Problem with Search function

    Hi, I can't seem to be able to figure out why this code:


    Code:
    char c[100];
    char buf[32];      
    
    ...
    
         if (fgets(buf, sizeof(buf), stdin)!= NULL)
                while (fgets(c, sizeof(buf), file)!= NULL) 
                    if (strstr(c, buf)!= NULL)
                        printf("searchword found!\n");
    
    ...
    doesn't show me that it found the first searchword.
    It works but it should find 6 but only shows up 5 "searchword found".

    Can anyone give me some tips?
    Last edited by tboy; 11-24-2004 at 08:08 AM.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    I'm confused.

    Code:
    char *buf
    sizeof(buf)
    doesn't make sense --
    because on a 32 bit system sizeof a pointer returns the size of a long - 4 bytes. In any case it returns more than sizeof char.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    opps let me change that...it was just a mistake which I forgot to change.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Code:
    char *s1 = "hello world!"
    char s2[]= "goodbye"
    
    sizeof(s1) = 4;
    sizeof(s2) = 8;
    This, because s1 is a pointer, and s2 is an array with space for 8 chars.

    Code:
     while (fgets(c, sizeof(buf), file)!= NULL)
    The 2nd parameters is wrong
    Code:
    while (fgets(c, sizeof(c), file)!= NULL)
    Although your code doesn't cause bufferoverflow, i think you meant to use sizeof(c).
    And before entering the while loop above be sure to place the file pointer at the begining, or you'll read nothing.
    Code:
    fseek(file,0,SEEK_SET);
    Last edited by xErath; 11-24-2004 at 08:13 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Well yes thanks for pointing out that mistake.
    But it still doesn't show up the first found entry.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > if (fgets(buf, sizeof(buf), stdin)!= NULL)
    This will store a newline as well, so unless you want to only match words at the end of a line, then you need to remove the newline.
    How to do this is in the FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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