C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2002, 10:32 PM   #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
tetradtech is offline   Reply With Quote
Old 10-09-2002, 10:47 PM   #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
starX is offline   Reply With Quote
Old 10-09-2002, 11:00 PM   #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
tetradtech is offline   Reply With Quote
Old 10-10-2002, 07:43 AM   #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
tetradtech is offline   Reply With Quote
Old 10-10-2002, 08:40 AM   #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!
tetradtech is offline   Reply With Quote
Old 10-10-2002, 10:58 AM   #6
Registered User
 
Join Date: Oct 2002
Posts: 13
size of temp

Code:
char temp = malloc(1000);
tetradtech is offline   Reply With Quote
Old 10-10-2002, 11:32 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
char Handling, probably typical newbie stuff Neolyth C Programming 16 06-21-2009 04:05 AM
Lame null append cause buffer to crash cmoo C Programming 8 12-29-2008 03:27 AM
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Another overloading "<<" problem alphaoide C++ Programming 18 09-30-2003 10:32 AM
lvp string... Magma C++ Programming 4 02-27-2003 12:03 AM


All times are GMT -6. The time now is 11:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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