Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sscanf() for just one field

sscanf() for just one field

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 9 9ine

    How to read from a string char str[] = "field1 field2 field3 ..." only one field3 string using sscanf() and one buffer for it - tmp. sscanf(str,"%s %s %s",0,0,tmp); it produces exception in this case

    9ine

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #2

    9ine wrote:

    sscanf(str,"%s %s %s",0,0,tmp);

    Replace zeroes by address of variables because sscanf expects some address to be passed in. You are passing in NULL hence the exception I guess.

    char str[] = "15 12 14";

    int num1,num2,num3;
    num1=num2=num3=0;

    sscanf(str, "%d %d %d", &num1, &num2, &num3 );
    printf( "Num1: %d, num2: %d, num3: %d", num1, num2, num3 );


    Nibu thomas A Developer Programming tips[^]  My site[^]

    9 1 Reply Last reply
    0
    • N Nibu babu thomas

      9ine wrote:

      sscanf(str,"%s %s %s",0,0,tmp);

      Replace zeroes by address of variables because sscanf expects some address to be passed in. You are passing in NULL hence the exception I guess.

      char str[] = "15 12 14";

      int num1,num2,num3;
      num1=num2=num3=0;

      sscanf(str, "%d %d %d", &num1, &num2, &num3 );
      printf( "Num1: %d, num2: %d, num3: %d", num1, num2, num3 );


      Nibu thomas A Developer Programming tips[^]  My site[^]

      9 Offline
      9 Offline
      9ine
      wrote on last edited by
      #3

      I want to use just one variable to its arguments list. Consider if I need to read 20th field from a string I dont want to declare int num1,num2,num3,num4, ........... num20 to read last field :-) 9ine

      Z 1 Reply Last reply
      0
      • 9 9ine

        How to read from a string char str[] = "field1 field2 field3 ..." only one field3 string using sscanf() and one buffer for it - tmp. sscanf(str,"%s %s %s",0,0,tmp); it produces exception in this case

        9ine

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        9ine wrote:

        sscanf(str,"%s %s %s",0,0,tmp);

        Use:

        sscanf(str, "%*s %*s %s", tmp);

        9ine wrote:

        it produces exception in this case

        As it should since you are trying to write to address 0.


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        9 S K 3 Replies Last reply
        0
        • D David Crow

          9ine wrote:

          sscanf(str,"%s %s %s",0,0,tmp);

          Use:

          sscanf(str, "%*s %*s %s", tmp);

          9ine wrote:

          it produces exception in this case

          As it should since you are trying to write to address 0.


          "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

          "Judge not by the eye but by the heart." - Native American Proverb

          9 Offline
          9 Offline
          9ine
          wrote on last edited by
          #5

          that is the nice one. I have not seen it in sscanf documentation

          9ine

          1 Reply Last reply
          0
          • 9 9ine

            I want to use just one variable to its arguments list. Consider if I need to read 20th field from a string I dont want to declare int num1,num2,num3,num4, ........... num20 to read last field :-) 9ine

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #6

            9ine wrote:

            Consider if I need to read 20th field from a string I dont want to declare int num1,num2,num3,num4, ........... num20 to read last field :-)

            sscanf isn't the right function for doing this. And using the %s option for sscanf isn't advised since it will almost never do what you hope it will. In fact, only your first %s will be read in and you will corrupt memory with the other 2 (even after fixing your references to the variables). If you are looking to access a single (or even a few) elements in a delimited string, write a split method, or use strtok to march through the array to the elements you want to access.

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            1 Reply Last reply
            0
            • D David Crow

              9ine wrote:

              sscanf(str,"%s %s %s",0,0,tmp);

              Use:

              sscanf(str, "%*s %*s %s", tmp);

              9ine wrote:

              it produces exception in this case

              As it should since you are trying to write to address 0.


              "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

              "Judge not by the eye but by the heart." - Native American Proverb

              S Offline
              S Offline
              Sebastian Schneider
              wrote on last edited by
              #7

              I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)

              Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

              T Z 9 3 Replies Last reply
              0
              • 9 9ine

                How to read from a string char str[] = "field1 field2 field3 ..." only one field3 string using sscanf() and one buffer for it - tmp. sscanf(str,"%s %s %s",0,0,tmp); it produces exception in this case

                9ine

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #8

                Or like this. N.B. Add error handling as required! char str[] = "field1 field2 field3 ..." // only one field3 string using sscanf() and one buffer for it - tmp. char * cpLastSpace = strrchr(str, (int) ' '); // Find the last space in str if(NULL != cpLastSpace) { sscanf(cpLastSpace+1,"%s",tmp); // Or strcpy(tmp, cpLastSpace+1); }

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                1 Reply Last reply
                0
                • S Sebastian Schneider

                  I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)

                  Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #9

                  Sebastian Schneider wrote:

                  I am gonna remove all stringstreams from my code, right now

                  are you serious :~ C++ classes are much prefered than C functions dude!


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  S 1 Reply Last reply
                  0
                  • D David Crow

                    9ine wrote:

                    sscanf(str,"%s %s %s",0,0,tmp);

                    Use:

                    sscanf(str, "%*s %*s %s", tmp);

                    9ine wrote:

                    it produces exception in this case

                    As it should since you are trying to write to address 0.


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    K Offline
                    K Offline
                    kakan
                    wrote on last edited by
                    #10

                    Nice. I didn't know that either.

                    Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                    1 Reply Last reply
                    0
                    • S Sebastian Schneider

                      I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)

                      Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #11

                      Sebastian Schneider wrote:

                      Wow. I am gonna remove all stringstreams from my code, right now

                      I do hope you were being sarcastic :-P sscanf is great when parsing typed information ... it is not very useful for trying to extract substrings.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      S 1 Reply Last reply
                      0
                      • S Sebastian Schneider

                        I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)

                        Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                        9 Offline
                        9 Offline
                        9ine
                        wrote on last edited by
                        #12

                        yeah. david crow rulez

                        9ine

                        1 Reply Last reply
                        0
                        • T toxcct

                          Sebastian Schneider wrote:

                          I am gonna remove all stringstreams from my code, right now

                          are you serious :~ C++ classes are much prefered than C functions dude!


                          TOXCCT >>> GEII power

                          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                          S Offline
                          S Offline
                          Sebastian Schneider
                          wrote on last edited by
                          #13

                          toxcct wrote:

                          are you serious

                          Hrhr.

                          Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                          1 Reply Last reply
                          0
                          • Z Zac Howland

                            Sebastian Schneider wrote:

                            Wow. I am gonna remove all stringstreams from my code, right now

                            I do hope you were being sarcastic :-P sscanf is great when parsing typed information ... it is not very useful for trying to extract substrings.

                            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            S Offline
                            S Offline
                            Sebastian Schneider
                            wrote on last edited by
                            #14

                            I was. Oh, and I noticed 2 typos in your Signature:

                            Zac Howland wrote:

                            ...for 8 hours a day, 5 days a week

                            should be

                            experience tells us:

                            ...for 11 hours a day, 6 days a week

                            Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                            Z 1 Reply Last reply
                            0
                            • S Sebastian Schneider

                              I was. Oh, and I noticed 2 typos in your Signature:

                              Zac Howland wrote:

                              ...for 8 hours a day, 5 days a week

                              should be

                              experience tells us:

                              ...for 11 hours a day, 6 days a week

                              Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                              Z Offline
                              Z Offline
                              Zac Howland
                              wrote on last edited by
                              #15

                              Just checking ... And I've thought about changing it to say "at least 8 hours ... 5 days" since my old job fit the 20 hour/7 day mold.

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              S 1 Reply Last reply
                              0
                              • Z Zac Howland

                                Just checking ... And I've thought about changing it to say "at least 8 hours ... 5 days" since my old job fit the 20 hour/7 day mold.

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                S Offline
                                S Offline
                                Sebastian Schneider
                                wrote on last edited by
                                #16

                                Gotta loooooove crunch-time...

                                Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                                Z 1 Reply Last reply
                                0
                                • S Sebastian Schneider

                                  Gotta loooooove crunch-time...

                                  Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

                                  Z Offline
                                  Z Offline
                                  Zac Howland
                                  wrote on last edited by
                                  #17

                                  Oh no. That was normal operating procedure ... When crunch time came around, the managers would pressure us to spend the night in the office ... even over weekends!

                                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups