strstr problem...

This is a discussion on strstr problem... within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write some structure searching functions. My main difficulty is my lack of knowledge on how ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    strstr problem...

    Hi, I am trying to write some structure searching functions. My main difficulty is my lack of knowledge on how to access these structures correctly, but for now here's my problem:

    I am trying to use strstr() to search for a specific word in the struct, but it fails to find even single characters I KNOW are there.

    ?????????????????????????????????????????????????
    PHP Code:
    void search(void)
     {
    char buf[1000];
       
    FILE *fpr;
      
    char check[100];
      
    char *found;

    printf("\nPlease enter a word to search for an instance of:");

    fgets(check,100,stdin);

    if((
    fpr=fopen("c:/MyFolder/Data.txt","r"))==NULL)
    {
    printf("Couldn't Open \"c:/MyFolder/Data.txt\" ! \n");exit(1);}

    fgets(buf,1000-1,fpr);

    found=strstr(buf,check);

    if(
    found)
    printf("Found!");

    else { 
    printf("Not found!!!!!!!!!!!!!!!!!");}

    fclose(fpr);
     } 
    ??????????????????????????????????????????????

    The output is always "Not found!!!!!!!!!!!!!!!!"

    Any Ideas?
    Last edited by Sebastiani; 08-24-2001 at 04:33 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Doesn't strstr return a pointer to a dynamically allocated copy of the original? I guess not... educational post.
    .sect signature

  3. #3
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    .sect signature

  4. #4
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    I don't know much about the C style I/O, but my guess was that fgets was reading the '\n' (read from stdin) character into your check string. I performed the following test which removed the '\n' from the "check" string, and it worked.

    Code:
    void search(void)
    {
       char buf[1000];
       char input[100];
       char *check;
       char *found;
       FILE *fpr;
    
       printf("\nPlease enter a word to search for an instance of:");
       fgets (input,100,stdin);
       check = strtok( input, "\n" );
    
       if ((fpr=fopen("Data.txt","r"))==NULL){
           printf("Couldn't Open \"c:/MyFolder/Data.txt\" ! \n");
           exit(1);
       }
    
       fgets (buf,1000-1,fpr);
       printf( "%s\n%s\n", buf, check);
    
       found = strstr(buf,check);
       printf( "%s", found );
    
       if (found)
          printf("Found!");
       else {
          printf("Not found!!!!!!!!!!!!!!!!!");
       }
    
       fclose(fpr);
    }
    like i said, i'm unfamiliar with C's input functions, but i would guess there's one that would ignore the '\n' from the user's input...

    mxr
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  5. #5
    Unregistered
    Guest
    Instead of having it simply say "Not Found", have it output the string to sdtout and see if it is finding what it should.

  6. #6
    Unregistered
    Guest

    Lightbulb

    Hi Sebastini
    mix0matt is right ........
    the problem with that program is the fgets function.
    Remember fgets reads only till the first "\n"(from any stream) . So if ur file is having newline character then it wont read what u wanted.

    After reading dump the file content to the screen and check ...
    happy coding
    Raghu R

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yup. I started programming in June or July of that year, so that really was a noob post.

    >> Certainly Salem telling me to not reply to posts that are over 7 years old will bring a tear to my eye..... But it was worth sharing.

    Probably.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. strstr problem...
    By Saggio in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2006, 10:40 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

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