for (i = 0;i < e;i++)
{       
                         // take the excuse
                         scanf(" %[^\n]", excuses[i]);
                        
                         excuse_count[i] = 0;
                         
                         while (sscanf(excuses[i], " %[a-zA-z]", word) != EOF)
}

The input was

My dog ate my homework

but at the while loop word is getting the word "My" in each loop. In case of scanf, when it reads from the stdin, each time it is called it reads and advances the read pointer. But I guess for sscanf the behavior is different. What I want is sscanf to act like scanf and read and advance the file pointer? Am I missing anything or this is not possible with sscanf? In that case how to accomplish this?

PS: Does anyone know why firefox is showing "behavior" as a wrong spelling while I am typing here in this text area?

char buff[] = "My dog ate my homework";
char *p = buff;
int n;
while ( sscanf( p, "%s%n", word, &n ) == 1 ) {
  p += n;  // advance through the buffer
}

> PS: Does anyone know why firefox is showing "behavior" as a wrong spelling while I am typing here in this text area?
You install the dictionary for your flavour of English.
So things like colour are spelled correctly ;)

commented: Then my science project ate my dog :P +8
commented: I've added [code]s after five days (below). Bad colour scheme. +9
char buff[] = "My dog ate my homework";
char *p = buff;
int n;
while ( sscanf( p, "%s%n", word, &n ) == 1 ) {
  p += n;  // advance through the buffer
}

> PS: Does anyone know why firefox is showing "behavior" as a wrong spelling while I am typing here in this text area?
You install the dictionary for your flavour of English.
So things like colour are spelled correctly ;)

Great. Your solution worked perfectly.

I have a related problem which I tried to do in 2 ways - 1 in your approach and another in my approach. But sadly I don't know why none of this worked.

The problem is that the input is a sequence of numbers on a line. More specifically a number of datasets like this:

3
1 7 6 4
5
3 8 0 8

where each dataset consists of a pair of lines. first line is one single number on a line by itself. In the second line, there is a sequence of numbers the length which is unknown. so obviously (as occurred to me), I should take the second line as a string and then do some while loop until it exhausts. So I did it the following:

approach 1

scanf("%d", first_no);
scanf("%[^\n]", input);
while (sscanf(input, "%d%[^\n]", &a[i], input) != EOF)
{
           i++;
}

but ironically the input has the same content each pass of the loop (I guess "7 6 4").

approach 2

char* p = input; // input obtained as above
int n;
while (sscanf(p, "%d%n", &a[i], &n) != EOF)
{
           p += n;
           i++;
}

This also had similar problem.

But I am not sure why the first approach didn't work. Because I used that approach before pretty long time ago which I recalled today.

Of course we could take your approach literally and get the token words and then use atoi to convert them to numbers. But why would %d not work?

Moreover, is there any elegant way of doing this like using variable arguments list (va_list) or so. I mean a sequence of unknown length of numbers and has to be got the numbers. As you see, EOF or NULL would not work because we have to determine the boundary of data sets which is not known.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.