User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,563 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,728 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1010 | Replies: 6
Reply
Join Date: Sep 2007
Posts: 20
Reputation: beatlea is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
beatlea beatlea is offline Offline
Newbie Poster

sscanf() in a loop

  #1  
Oct 16th, 2007
Hello,
Is there any way of reading words from a string containg a few words separated by white spaces using a loop? I don't know how many argumets the user will input. I wanted to do it like this:

    
for(i=0;i<wordCounter;i++)
{
  sscanf(input,"%s",argument[i]);
}

But this keeps reading in the first word from the string.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: sscanf() in a loop

  #2  
Oct 16th, 2007
You can use the %n conversion specifier to find out how many characters were read and adjust the source string accordingly:
#include <stdio.h>

int main( void )
{
  const char *src = "this is a test";
  char word[5];
  int n;

  while ( sscanf ( src, "%4s%n", word, &n ) == 1 ) {
    puts ( word );
    src += n;
  }

  return 0;
}
Note that the usual guideline of using the number of conversion specifiers to determine what value scanf should return doesn't apply here. %n doesn't add to the number of conversions, which is why the loop uses 1 instead of 2 to mark success.
Last edited by Narue : Oct 16th, 2007 at 4:03 pm.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Sep 2007
Posts: 20
Reputation: beatlea is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
beatlea beatlea is offline Offline
Newbie Poster

Re: sscanf() in a loop

  #3  
Oct 16th, 2007
Sorry, but I don't understand this code Why do you have to use puts(word) after reading in to word with sscanf? And what does += do?
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: sscanf() in a loop

  #4  
Oct 16th, 2007
>Sorry, but I don't understand this code
Gone are the days when rookies would take a new piece of code and run off to dissect it.

>Why do you have to use puts(word) after reading in to word with sscanf?
Because it's generally a better example if you can see that it's working.

>And what does += do?
It does exactly what your book on C tells you it does: adds the value on the right to the value on the left.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Sep 2007
Posts: 20
Reputation: beatlea is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
beatlea beatlea is offline Offline
Newbie Poster

Re: sscanf() in a loop

  #5  
Oct 16th, 2007
Yes, I did try it and all I got was a compiler error. It doesn't like src += n;

error: invalid operands to binary +
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: sscanf() in a loop

  #6  
Oct 16th, 2007
>Yes, I did try it and all I got was a compiler error.
Post the exact code you tried then, because you messed it up somehow. Also mention which compiler and OS you're using.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,257
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: sscanf() in a loop

  #7  
Oct 16th, 2007
Sounds like bad copying on your part, replacing a pointer with an array.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC