Extracting a string from a buffer?

This is a discussion on Extracting a string from a buffer? within the Linux Programming forums, part of the Platform Specific Boards category; Again, I'm really new at this. I've tried searching on strings, strstr, etc. Stuff I found didn't seem to apply. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Unhappy Extracting a string from a buffer?

    Again, I'm really new at this. I've tried searching on strings, strstr, etc. Stuff I found didn't seem to apply.

    Code:
    char *domainResponse=malloc(1000);
    
    while(fgets(domainResponse, 1000, fp) != NULL)
      {
        /* String function here to search for 'no match for domain' */
        /*fputs(domainResponse, stdout);*/
     	
      }
      printf("%s\n", domainResponse);
    I get nothing printed out.

    Now, if I remove the comments from line 3 like this:

    Code:
    char *domainResponse=malloc(1000);
    
    while(fgets(domainResponse, 1000, fp) != NULL)
      {
        /* String function here to search for 'no match for domain' */
        fputs(domainResponse, stdout);
    	        	
      }
      /*printf("%s\n", domainResponse);*/
    I get the results of the bw-whois lookup printed to the screen (actually, a html page)

    I tried a strstr(domainResponse, "No match for domain") - but it always returned NULL, which led me to check what was in domainResponse. Seems if I do the fputs, domainResponse contains text; but if I just try to printf it, domainResponse seems empty. With the fputs operation, domainResponse has a long text paragraph.

    Please help.

    Thanks,
    Tim

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    did you try it this way?:
    Code:
    while(fgets(domainResponse, 1000, fp))
    {
    .
    .
    .
    }
    starX
    www.axisoftime.com

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    After reading your post, yes...

    After I read your reply, I tried as you suggested. Using the code with the fputs() commented out and the printf() attempting to print domainResponse.

    Same result. I'll leave as is and you can see - if you'd like.

    http://www.tetradtech.com/domain_check.htm

    What you should see on the page after you click on "submit" would be a paragraph responding with whether the domain was available or not.

    Unfortunately, that doesn't show up.

    Thanks,
    Tim

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Not a *duh* IMHO

    Again, I'm new at this.

    But when you place the results from stdout or stdin into a variable, that variable should contain whatever was read in, yes?

    Whether domainResponse is in the while() statement or wherever, it should contain the results of the domain search.

    But I think I'm on to something here, when I moved the fgets() out of the while() and the printf() out of the while(), I only get one line printed out. That's because fgets() reads until it encounters a '\0', correct?

    So maybe the bw-whois is producing a newline at the end, and that's the last thing in the domainResponse variable.

    -Tim

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Found the problem...

    That was it...helps to print results to see what's actually happening and to include <br> in your html to see if text is split or one long paragraph. Apparently, the bw-whois prints the output as 4 or 5 separate paragraphs separated by newlines, so the last input into the buffer was a newline.

    This bit of code fixed the problem and it's FINALLY working. Thanks again for all of your help to this error-prone newbie.

    Code:
    while(fgets(domainResponse, 1000, fp))
      {
        strcat(temp, domainResponse);
    		
      }
      	
      printf("<br>\n");
      if(strstr(temp, "No match for domain")!=NULL)
      {
        printf("<b>The domain: %s is available!</b>\n", cgivars[1]);
      }
      else
      {
        printf("<b>The domain: %s is unavailable.</b>\n", cgivars[1]);
      }
    Thanks again!

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    size of temp

    Code:
    char temp = malloc(1000);

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    So why not do strstr on each line as you read it (then set a flag if you find it), rather than risk a buffer overflow with all those strcat's to temp
    Code:
    int found = 0;
    while( !found && fgets(domainResponse, 1000, fp) ) {
      found = strstr( domainResponse, "No match for domain") != NULL ; 
    }
    if ( found ) {
      printf("<b>The domain: %s is available!</b>\n", cgivars[1]);
    } else {
      printf("<b>The domain: %s is unavailable.</b>\n", cgivars[1]);
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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