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

    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