Mahmoud Aladdin
Mahmoud Aladdin

Reputation: 546

Splitting a white-spaced string using sscanf()

I'm trying to split a long string with white-spaces, using sscanf().

For example: I need to split this

We're a happy family

into

We're
a
happy
family

I tried the following approach

char X[10000];
fgets(X, sizeof(X) - 1, stdin); // Reads the long string
if(X[strlen(X) - 1] == '\n') X[strlen(X) - 1] = '\0'; // Remove trailing newline
char token[1000];
while(sscanf(X, "%s", token) != EOF) {
    printf("%s | %s\n", token, X);
}

The previous code goes into an infinite loop outputting We're | We're a happy family

I tried to replace sscanf() with C++ istringstream, it worked fine.

What makes X keep it's value? Shouldn't it be removed from buffer like normal stream?

Upvotes: 2

Views: 1488

Answers (1)

hmjd
hmjd

Reputation: 121971

sscanf() does store information about the buffer it previously read from and will always start from the address (buffer) passed to it. A possible solution would be to the use %n format specifier to record the position the last sscanf() stopped at and pass X + pos as the first argument to sscanf(). For example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    const char* X = "hello again there";
    char token[1000];
    int current_pos = 0;
    int pos = 0;
    while (1 == sscanf(X + current_pos, "%999s%n", token, &pos))
    {
        current_pos += pos;
        printf("%s | %s\n", token, X + current_pos);
    }
    return 0;
}

See demo at http://ideone.com/XBDTWm .

Or just use istringstream and std::string:

std::istringstream in("hello there again");
std::string token;
while (in >> token) std::cout << token << '\n';

Upvotes: 2

Related Questions