GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jan-2005, 07:59
Shaikun Shaikun is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Shaikun is on a distinguished road

eating single space in scanf


Since a space in scanf()'s format matches all white-space, I was wondering if there is a way to make scanf() eat a single space in the middle of a format? (Yes, yes, I will be using sscanf, not scanf itself =)
  #2  
Old 15-Jan-2005, 08:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Shaikun
Since a space in scanf()'s format matches all white-space, I was wondering if there is a way to make scanf() eat a single space in the middle of a format? (Yes, yes, I will be using sscanf, not scanf itself =)

Give an example of the input line (string) and what you want to do with it.

Regards,

Dave
  #3  
Old 15-Jan-2005, 08:28
Shaikun Shaikun is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Shaikun is on a distinguished road
It's basically for a simple HTTP daemon, for parsing requests. Example of a good request would be "GET /foo HTTP/1.0" where all white-space are single spaces, but "GET /foo HTTP/1.0" is an invalid request because of the multiple spaces. I guess I could just walk through the buffer char by char (the request string is already in a buffer) but using sscanf() seemed like a better idea, or at least would be if it's able to detect single spaces.
  #4  
Old 15-Jan-2005, 09:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Shaikun
It's basically for a simple HTTP daemon, for parsing requests. Example of a good request would be "GET /foo HTTP/1.0" where all white-space are single spaces, but "GET /foo HTTP/1.0" is an invalid request because of the multiple spaces. I guess I could just walk through the buffer char by char (the request string is already in a buffer) but using sscanf() seemed like a better idea, or at least would be if it's able to detect single spaces.
Since sscanf() actually removes whitespace, you could read the string into multiple smaller strings then reassemble with only one space between.

Another idea is to use sscanf()'s "read until whitespace" functionality and set up a loop until you're out of string. But after each read you must remove what was read from the original buffer which can be a fair amount of work.

A third way (and better in my opinion), is to move the string char by char into another buffer, ignoring multiple whitespace. It's easiest, faster, and you're sure to get it right.

See the tutorial Things to avoid in C/C++ for a discussion on scanf(), which also goes for sscanf() for the most part.
__________________

Age is unimportant -- except in cheese
  #5  
Old 15-Jan-2005, 16:11
Shaikun Shaikun is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Shaikun is on a distinguished road
Char-by-char it is then =) Things like this make me long for perl. Thanks for your reply!
  #6  
Old 16-Jan-2005, 00:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Shaikun
Things like this make me long for perl. Thanks for your reply!

If you design this task as a function, keep it handy. You now have your "perl-like" function. After all, that's probably all perl does. As you develop more functions that seem useful, keep them in a special library directory so you can find and use them again.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
single doc interface in MFC vadharah MS Visual C++ / MFC Forum 0 26-May-2004 04:10
Art and Maths.....a single hypervoxel paulselhi Graphics Forum 0 10-Mar-2004 06:11
600MB Space, 20GB Banwidth, PHP, MySQL, CGI, SSH, FrontPage, FTP, POP3, Web Mail fcolor Web Hosting Advertisements & Offers 0 08-Aug-2003 10:11
Hosting under $8/month: 10-20GB Transfer, 300-600MB Space, PHP, MySQL, Domain Name! fcolor Web Hosting Advertisements & Offers 0 28-Feb-2003 04:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.