Thread: Using strtok and sscanf

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    11

    Using strtok and sscanf

    I am trying to split an array up into tokens, an array that is input by the user, and from those tokens extract numbers only. Here is my code so far, but I am having some problems. It prints out the numbers but also some other stuff... Here is the code and the output.

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

    int main() {

    char delimiters[] = " \t\n\r";
    char storage[100];
    char copy[100];
    int* p;
    int n=0, numbers;

    for( n=0; storage[n] != EOF; n++ ) {
    fgets( storage, sizeof(storage), stdin );
    strcpy(copy, storage);
    p = strtok(copy, delimiters);
    while (p) {
    sscanf(p, "%d", &numbers);
    printf("%d\t", numbers);
    p = strtok(NULL, delimiters);
    }
    printf("\n");
    }
    }

    Output:

    % gcc -o test test.c
    test.c: In function `main':
    test.c:15: warning: assignment makes pointer from integer without a cast
    test.c:17: warning: passing arg 1 of `sscanf' from incompatible pointer type
    test.c:19: warning: assignment makes pointer from integer without a cast
    % test
    hey 3are some 23 numbers
    3 3 3 23 23

    Ive been over this for the past few days and cant seem to get anywhere, nor can i find anything in my books. Thanks for your time and help in advance!

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You need to include string.h, and change p to a char pointer, that should take care of the syntax problems.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    11

    help!

    i did that but i still get the same problems. Whats happening is this...

    % test
    what 45 2k
    2 45 2

    Why is my program printing a 2 for the first token when there is no numbers in it? Here is my code again fixed up:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    	
    	char delimiters[] = " \t\n\r";
    	char storage[100];
    	char copy[100];
    	char* p;
    	int n=0, x;
    	double numbers;
    
    	for( n=0; storage[n] != NULL; n++ ) {
    		fgets( storage, sizeof(storage), stdin );
    		strcpy(copy, storage);
    		p = strtok(copy, delimiters);
    		while (p) {
    			x = sscanf(p, "%lf", &numbers);
    			printf("%f\t", numbers);
    			p = strtok(NULL, delimiters);
    		}
    		printf("\n");
    	}
    }
    Tagged by Salem
    http://cboard.cprogramming.com/misc....bbcode#buttons

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    AFAIK, the way scanf handles invalid input (i.e. A2 for a %lf) is undefined. You should look at the value of x after the scanf and see what its value is.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Throw in a few debug lines. Use the return value of sscanf. Seriously, why have x if you don't use it?

    printf("---\nx is %d\np is %s\n---\n", x, p );

    [edit]Beat me to it.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    11

    sweet!

    Finally I got it working. Thanks everyone for their help. I took out that loop, well for the moment, and Ill look it over. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing using strtok() and sscanf()
    By NuNn in forum C Programming
    Replies: 13
    Last Post: 02-12-2009, 02:43 PM
  2. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  3. sscanf() examples?
    By Axel in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 10:00 AM
  4. sscanf
    By paperbox005 in forum C Programming
    Replies: 2
    Last Post: 08-18-2004, 06:46 AM
  5. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 04:56 PM