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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sscanf problem

sscanf problem

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 2 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.
  • A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #1

    I am trying to read values from from a text file using sscanf. The file contains numberic values but I want to read them in as strings, each line at a time. Values are listed as shown: example text file:

    1.000 2.000
    3.000 4.000
    5.000 6.000
    ...

    Here is the code. CStdioFile LFileData; ... LFileData.ReadString(LCStrReadStr); MessageBox(LCStrReadStr); sscanf(LCStrReadStr, "%s %s", LCStrTemp1, LCStrTemp2); The problem is that both LCStrTemp1 and LCStrTemp2 is getting the value of the 2nd column only. The first column is lost. I outputted LCStrReadStr using MessageBox to make sure it was correct. It is. So that problem is in sscanf. Why am I not getting the right values? Thanks in advance.

    M 1 Reply Last reply
    0
    • A acerunner316

      I am trying to read values from from a text file using sscanf. The file contains numberic values but I want to read them in as strings, each line at a time. Values are listed as shown: example text file:

      1.000 2.000
      3.000 4.000
      5.000 6.000
      ...

      Here is the code. CStdioFile LFileData; ... LFileData.ReadString(LCStrReadStr); MessageBox(LCStrReadStr); sscanf(LCStrReadStr, "%s %s", LCStrTemp1, LCStrTemp2); The problem is that both LCStrTemp1 and LCStrTemp2 is getting the value of the 2nd column only. The first column is lost. I outputted LCStrReadStr using MessageBox to make sure it was correct. It is. So that problem is in sscanf. Why am I not getting the right values? Thanks in advance.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What are LCStrTemp1 and LCStrTemp2? They don't point to the same place do they? Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        What are LCStrTemp1 and LCStrTemp2? They don't point to the same place do they? Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        A Offline
        A Offline
        acerunner316
        wrote on last edited by
        #3

        They are both CString declared like this. So they shouldn't be pointing to the same thing. CString LCStrTemp1; CString LCStrTemp2;

        M 1 Reply Last reply
        0
        • A acerunner316

          They are both CString declared like this. So they shouldn't be pointing to the same thing. CString LCStrTemp1; CString LCStrTemp2;

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          You'll need to pass char* arguments to sscanf()...

          // example
          char TestString[] = "1.000 2.000";
          char StrTemp1[80];
          char StrTemp2[80];
          sscanf(TestString, "%79s %79s", StrTemp1, StrTemp2);
          CString LCStrTemp1 = StrTemp1;
          CString LCStrTemp2 = StrTemp2;

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            You'll need to pass char* arguments to sscanf()...

            // example
            char TestString[] = "1.000 2.000";
            char StrTemp1[80];
            char StrTemp2[80];
            sscanf(TestString, "%79s %79s", StrTemp1, StrTemp2);
            CString LCStrTemp1 = StrTemp1;
            CString LCStrTemp2 = StrTemp2;

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            A Offline
            A Offline
            acerunner316
            wrote on last edited by
            #5

            oh that worked! Thanks. So I guess CString doesn't replace all string requirements...

            M 1 Reply Last reply
            0
            • A acerunner316

              oh that worked! Thanks. So I guess CString doesn't replace all string requirements...

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              acerunner316 wrote:

              So I guess CString doesn't replace all string requirements...

              Not with old-school ANSI C functions like sscanf :) The problem is with matching the argument(s) to the format specifier(s). The %s format specifier means it'll look for a char*, and this char* must point to a buffer as large as you specify or large enough to hold all expected scanned characters (or it will crash). Although CString lets you access its internal buffer, you'd have to be sure that buffer is allocated and is big enough. *edit* Oh yeah, there's CString::Tokenize() :) Have fun! Mark -- modified at 20:50 Thursday 3rd May, 2007

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              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