Thread: parse string in C using sscanf

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    2

    parse string in C using sscanf

    I want to parse $100,50,30# using sscanf and store 100 50 and 30 into seperate variables. How can i do it...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose you had those three variables. Do you know how to print them in that format using printf? If so, show an example code snippet.

    Suppose you had the string "123". Do you know how to use sscanf to parse it into an int variable? If so, show an example code snippet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    2

    parsing string and converting it to int

    I can do something like this:


    char *str= "$100,50,40#";

    char buf1[1], buf2[1],buf3[1];

    sscanf(str, "%[^','],%[^','],%s", buf1,buf2,buf3);

    int i = atoi(buf1);
    int j = atoi(buf2);
    int k = atoi(buf3);

    but the problem is sscanf can parse and ignore , and store the tokens in buf1, buf2, and buf3....but it cant remove $ and #....i need a code to remove all the delimeters from str and store it in seperate variables..that is what i want and that too using sscanf...any help would be appreciable

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of making use of buf1, buf2 and buf3, try reading into i, j and k directly, with the help of %d. Remember to check that sscanf returns 3 as there could be a format error in the input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @OP: When posting code, you should use code tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using sscanf to parse string
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 06:20 PM
  2. how to parse a string
    By -EquinoX- in forum C Programming
    Replies: 48
    Last Post: 05-21-2008, 01:57 PM
  3. sscanf does'nt parse a simple file correctly
    By seidel in forum C Programming
    Replies: 2
    Last Post: 02-12-2008, 07:15 AM
  4. how to parse a string?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 01-10-2007, 02:49 PM
  5. Parse the string
    By swanley007 in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2005, 11:17 AM