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. Problems with CString and fscanf

Problems with CString and fscanf

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • J Offline
    J Offline
    Jose Luis Sogorb
    wrote on last edited by
    #1

    Why does this code work?: char test[12], test1[12]; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); And this doesn`t: CString test, test1; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); The CString values are correct just after the 'fscanf', but they disappear after some other function calls.

    M 1 Reply Last reply
    0
    • J Jose Luis Sogorb

      Why does this code work?: char test[12], test1[12]; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); And this doesn`t: CString test, test1; ....more code... fscanf(fp,"%d %s %s %s %s",&hour,station,country,test,test1); The CString values are correct just after the 'fscanf', but they disappear after some other function calls.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      You are directly modifying the contents of the CString without locking the buffer first.

      CString test, test1;
      LPTSTR pszBuff, pszBuff1;

      pszBuff = test.GetBuffer(12);
      pszBuff1 = test1.GetBuffer(12);

      Now you can use pszBuff/pszBuff1 in fscanf(). Call ReleaseBuffer() on both CStrings once you're done directly modifying the contents. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

      J 1 Reply Last reply
      0
      • M Michael Dunn

        You are directly modifying the contents of the CString without locking the buffer first.

        CString test, test1;
        LPTSTR pszBuff, pszBuff1;

        pszBuff = test.GetBuffer(12);
        pszBuff1 = test1.GetBuffer(12);

        Now you can use pszBuff/pszBuff1 in fscanf(). Call ReleaseBuffer() on both CStrings once you're done directly modifying the contents. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

        J Offline
        J Offline
        Jose Luis Sogorb
        wrote on last edited by
        #3

        Thank you, so much. And then if I modify the values in many places, this way: if (...) test="some word", and so on... What must I do? Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one?

        M 1 Reply Last reply
        0
        • J Jose Luis Sogorb

          Thank you, so much. And then if I modify the values in many places, this way: if (...) test="some word", and so on... What must I do? Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          José Luis Sogorb wrote: Have I to make a GetBuffer before modifying it and ReleaseBuffer after each one? Yep. See the docs on CString::GetBuffer() for some examples. --Mike-- My really out-of-date homepage "Why does anyone have a web page? Too much free time... not enough friends... justifying owning a computer." -- Noel Crane on Felicity Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

          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