Christopher Shroba
Christopher Shroba

Reputation: 7584

sscanf not working right?

I am trying to parse a URL, and wrote this code:

#include <stdio.h>

int main() {
    char host[100];
    char port[100];
    char path[100];
    char prot[100];
    char* url = "https://google.com:8000/foobar";
    sscanf(url, "%s://%s:%s/%s", prot, host, port, path);
    printf("Protocol: %s\n", prot);
    printf("Host:     %s\n", host);
    printf("Port:     %s\n", port);
    printf("Path:     %s\n", path);

    return 0;
}

However, it outputs this:

Protocol: https://google.com:8000/foobar
Host:     å0&TFaa
Port:     
Path:

I'm not sure why it is putting all of my string into the protocol variable, instead of putting the correct parts into each variable. Any ideas?

Upvotes: 2

Views: 1170

Answers (2)

Ed Heal
Ed Heal

Reputation: 60037

The sscanf() format with "%s" is greedy so it will match as much as possible.

You also need to check the return value of sscanf() It is returning one.

Please look up the manual page. Perhaps the format "%[^:]" is what you are looking for.

Upvotes: 7

R Sahu
R Sahu

Reputation: 206717

sscanf is greedy. It reads as many characters as it can.

Change it to use:

char* url = "https://google.com:8000/foobar";
sscanf(url, "%[^:]://%[^:]:%[^/]/%s", prot, host, port, path);

Upvotes: 9

Related Questions