Thread: Using sscanf

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

    Using sscanf

    I am trying to use sscanf to print out only the numbers in a string of characters. Here is my code so far, but I get a segmentation fault when i run it.

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

    int main() {
    char storage[100];
    int n=0, numbers;
    while( storage[n] != EOF ) {
    gets(storage);
    sscanf(storage[n], "%lf", &numbers);
    printf("%d\n", numbers);
    n++;
    }
    }

    I cant seem to find the error in this code, please help me!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The first parameter to sscanf() takes a c-string, not a char - it also expects a pointer to store the results. You are also mixing integers and floats with %d and %lf (and using EOF incorrectly).
    Code:
        char test[] = "1234";
        int n;
        sscanf(test,"%d",&n);
        printf ("%d\n",n);
    What are you trying to do? Maybe we can point you to a tutorial. Otherwise, study your C book.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM