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. using sscanf in a loop

using sscanf in a loop

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 3 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.
  • B Offline
    B Offline
    b rad311
    wrote on last edited by
    #1

    Hi, How can I advance the sscanf statement to read each word in the sentence? Right now my output is an endless loop of "This".

    CString incoming_line="This is my example incoming line.";
    CString string_out;
    while (sscanf(incoming_line, "%s", string_out )==1)
    {
    cout<<string_out<<endl;
    }

    Thanks!

    C D 2 Replies Last reply
    0
    • B b rad311

      Hi, How can I advance the sscanf statement to read each word in the sentence? Right now my output is an endless loop of "This".

      CString incoming_line="This is my example incoming line.";
      CString string_out;
      while (sscanf(incoming_line, "%s", string_out )==1)
      {
      cout<<string_out<<endl;
      }

      Thanks!

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you could do something like this:

      int offset = 0;
      while (sscanf(incoming_line + offset, "%s", string_out )==1)
      {
      cout<
      image processing toolkits | batch image processing

      B 1 Reply Last reply
      0
      • C Chris Losinger

        you could do something like this:

        int offset = 0;
        while (sscanf(incoming_line + offset, "%s", string_out )==1)
        {
        cout<
        image processing toolkits | batch image processing

        B Offline
        B Offline
        b rad311
        wrote on last edited by
        #3

        Chris, Thanks for your help. I'm getting the following error when I use your suggestion.

        Error 2 error C2666: 'ATL::CStringT<BaseType,StringTraits>::operator +' : 3 overloads have similar conversions

        Any ideas? Thanks,

        C 1 Reply Last reply
        0
        • B b rad311

          Hi, How can I advance the sscanf statement to read each word in the sentence? Right now my output is an endless loop of "This".

          CString incoming_line="This is my example incoming line.";
          CString string_out;
          while (sscanf(incoming_line, "%s", string_out )==1)
          {
          cout<<string_out<<endl;
          }

          Thanks!

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

          b-rad311 wrote:

          while (sscanf(incoming_line, "%s", string_out )==1)

          You cannot use a CString object in this capacity.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          B 1 Reply Last reply
          0
          • D David Crow

            b-rad311 wrote:

            while (sscanf(incoming_line, "%s", string_out )==1)

            You cannot use a CString object in this capacity.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Man who follows car will be exhausted." - Confucius

            B Offline
            B Offline
            b rad311
            wrote on last edited by
            #5

            Hi David, Should I use "const char*" instead?

            D 1 Reply Last reply
            0
            • B b rad311

              Chris, Thanks for your help. I'm getting the following error when I use your suggestion.

              Error 2 error C2666: 'ATL::CStringT<BaseType,StringTraits>::operator +' : 3 overloads have similar conversions

              Any ideas? Thanks,

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              oops. didn't see the CString there.

              image processing toolkits | batch image processing

              1 Reply Last reply
              0
              • B b rad311

                Hi David, Should I use "const char*" instead?

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

                If you did, then how would scanf() be able to change its contents? Since you are using MFC, have a look at CStringT::Tokenize() or AfxExtractSubString().

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                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