sscanf() issues

This is a discussion on sscanf() issues within the C Programming forums, part of the General Programming Boards category; Hello, I would like to check a string, using sscanf, for the format: "http://host:Port/path" I can't seem to figure it ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Question sscanf() issues

    Hello,
    I would like to check a string, using sscanf, for the format: "http://host:Port/path"

    I can't seem to figure it out. What format should I use?
    I currently have the following, but it returns 1 instead of 3
    Code:
    sscanf(someString,"http://%s:%d/%s", &host, &port, &path);
    Any idea?
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't. Use strstr() instead.

    sscanf() puts a value into a variable, but you don't want to do that here. You want to find a string, within a larger string - and that is included in string.h and called strstr().

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Well, I would to extract the data from the string. Sorry if I wasn't clear. I want to extract the protocol, the host and the port.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The details of it, depend on the details of the whole original string. Can you post an example of the original string?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by Adak View Post
    The details of it, depend on the details of the whole original string. Can you post an example of the original string?
    http://www.awebsite.com:80/index.html

    Ok, I have a bit of a progress here. This looks better, but I'm missing something here, and I dont understand how this really works. I get a different number in port though:
    Code:
    sscanf(someString,"http://%[^:]:%d%s",&host,port,&path);

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > sscanf(someString,"http://%[^:]:%d%s",&host,port,&path);
    Well it looks like you removed the & from the wrong one.

    Assuming two char arrays, and an int, it would be
    sscanf(someString,"http://%[^:]:%d%s",host,&port,path);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Not char array, but simply 2 char pointers. Why does this give me zero in port?
    Code:
    	char* host;
    	int port;
    	char* path;
    	
    	sscanf(someString,"http://%[^:]:%d/%[^\n]",&host,&port,&path);
    	printf("%s\n",&host);
    	printf("%d\n",port);
    	printf("%s\n",&path);

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Because using & on a char pointer to read in a string is just plain wrong.

    Which compiler are you using?
    If it's gcc / mingw, then use the -Wall flag.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Code:
    char* host;
    int port;
    char* path;
    	
    sscanf(someString,"http://%[^:]:%d/%[^\n]",&host,&port,&path);
    printf("%s\n",&host);
    printf("%d\n",port);
    printf("%s\n",&path);
    Is that really your code as written? Because you can't read into pointers that aren't pointing anywhere, as you're doing with host and path.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by rags_to_riches View Post
    Code:
    char* host;
    int port;
    char* path;
    	
    sscanf(someString,"http://%[^:]:%d/%[^\n]",&host,&port,&path);
    printf("%s\n",&host);
    printf("%d\n",port);
    printf("%s\n",&path);
    Is that really your code as written? Because you can't read into pointers that aren't pointing anywhere, as you're doing with host and path.
    I know I'm doing something wrong, obviously, this is not working. Can someone please show me the right way of doing this? not just about the parameters in sscanf, but also the structure of the format. "http://%s:%d/%s" is not good. Can someone shed a light on this as well?
    Thanks.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    OK guys, It's working now. Can someone please explain to me, since I would like to actually learn what happend here, why this is the right format?

    Code:
    	char host[100];
    	char path[100];
    	int port;
    	
    	sscanf(argv[1],"http://%[^:]:%d%*[/]%s",host,&port,path);
    	printf("%s\n",host);
    	printf("%d\n",port);
    	printf("%s\n",path);
    Thanks.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    What can you tell us about

    char path[100];

    vs.

    char *path = malloc( 100 );

    vs.

    char *path = NULL;


    It's also worth trying this.
    printf("%p %p %lu\n", (void*)path, (void*)&path, (unsigned long)(sizeof(path)) );

    depending on whether path is an array or a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() help
    By cyberfish in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 01:33 PM
  2. sscanf help!!
    By PunkyBunny300 in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 01:45 AM
  3. sscanf
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 02-11-2003, 05:58 PM
  4. sscanf
    By mickle in forum C Programming
    Replies: 3
    Last Post: 01-10-2003, 02:30 PM
  5. sscanf and gets
    By Max in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 07:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21