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. Another map question

Another map question

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
8 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.
  • N Offline
    N Offline
    ns
    wrote on last edited by
    #1

    The map is (long, char*). I get a char* buffer tempB from a CFile read, of size nSize. CAn I do aMap[nSize] = tempB? i.e. assign it with the = sign? or do I have to allocate memory for the maps second element or something and do a strcpy? I would have avoided char* and used CStrings but it looks like the Write for CFile needs a pointer to a buffer.... In case my question isnt clear...we cant assign char* with the = sign, it has to be strcpy. SO in the case of my map, do I allocate ,memory to *it.second() and strcpy? :confused: Appreciate your help, ns

    C 1 Reply Last reply
    0
    • N ns

      The map is (long, char*). I get a char* buffer tempB from a CFile read, of size nSize. CAn I do aMap[nSize] = tempB? i.e. assign it with the = sign? or do I have to allocate memory for the maps second element or something and do a strcpy? I would have avoided char* and used CStrings but it looks like the Write for CFile needs a pointer to a buffer.... In case my question isnt clear...we cant assign char* with the = sign, it has to be strcpy. SO in the case of my map, do I allocate ,memory to *it.second() and strcpy? :confused: Appreciate your help, ns

      C Offline
      C Offline
      Chad Koehler
      wrote on last edited by
      #2

      You can still use CString. CString csWrite(_T("Hello World")); CFile cFile; cFile.Open(...); cFile.Write((void*)(LPCTSTR)csWrite, csWrite.GetLength());

      N 1 Reply Last reply
      0
      • C Chad Koehler

        You can still use CString. CString csWrite(_T("Hello World")); CFile cFile; cFile.Open(...); cFile.Write((void*)(LPCTSTR)csWrite, csWrite.GetLength());

        N Offline
        N Offline
        ns
        wrote on last edited by
        #3

        Thanks so much for telling me CString works! I was going through hoops wqith strcpy and all that mess. Thank you very very much! Appreciate your help, ns

        N 1 Reply Last reply
        0
        • N ns

          Thanks so much for telling me CString works! I was going through hoops wqith strcpy and all that mess. Thank you very very much! Appreciate your help, ns

          N Offline
          N Offline
          ns
          wrote on last edited by
          #4

          Can I use the same trick with Read?CString abc; cFile.Read((void*)(LPCTSTR)abc, abc.GetLength()); Appreciate your help, ns

          S 1 Reply Last reply
          0
          • N ns

            Can I use the same trick with Read?CString abc; cFile.Read((void*)(LPCTSTR)abc, abc.GetLength()); Appreciate your help, ns

            S Offline
            S Offline
            Scott H Settlemier
            wrote on last edited by
            #5

            int n=S.GetLength(); // this is icky S.ReleaseBuffer(File.Read(S.GetBuffer(n),n));

            N 1 Reply Last reply
            0
            • S Scott H Settlemier

              int n=S.GetLength(); // this is icky S.ReleaseBuffer(File.Read(S.GetBuffer(n),n));

              N Offline
              N Offline
              ns
              wrote on last edited by
              #6

              Oh. So I dont need the LPTCTSR thing for read because GetBuffer returns the pointer to the string? Thanks for the compact coding. Appreciate your help, ns

              S 1 Reply Last reply
              0
              • N ns

                Oh. So I dont need the LPTCTSR thing for read because GetBuffer returns the pointer to the string? Thanks for the compact coding. Appreciate your help, ns

                S Offline
                S Offline
                Scott H Settlemier
                wrote on last edited by
                #7

                Argh. I can't stand it any longer. Just leaving with a note that the above is "icky" wont make the itch go away. I wrote something knowingly bad above. :-O This may be pedantic but I gotta get rid of the itch. :) GetBuffer returns a pointer to a buffer to which you may safely store directly into the CString object, but using a CString whose length happens to match the amount of data you are reading in is just bad practice. If one knows the amount of data to be read, then one should specify it explicitly. e.g. int n=128; // 128 byte field for planet name CString Planet; // read all 128 bytes and set CString to just the 0-terminated portion Planet.ReleaseBuffer(File.Read(Planet.GetBuffer(n),n)!=n?0:-1); Better yet, create structures that mirror the fixed length portions of the file and read them in entirety. e.g. struct Names { enum {NAMESIZE=128}; char Planet[NAMESIZE]; char System[NAMESIZE]; // etc. }; ABC MyNames; VERIFY(File.Read(&MyNames,sizeof(MyNames))==sizeof(MyNames)); and if you need it in a CString later: MyNames.Planet[NAMESIZE-1]=0; // make sure 0-terminated CString S=MyNames.Planet;

                N 1 Reply Last reply
                0
                • S Scott H Settlemier

                  Argh. I can't stand it any longer. Just leaving with a note that the above is "icky" wont make the itch go away. I wrote something knowingly bad above. :-O This may be pedantic but I gotta get rid of the itch. :) GetBuffer returns a pointer to a buffer to which you may safely store directly into the CString object, but using a CString whose length happens to match the amount of data you are reading in is just bad practice. If one knows the amount of data to be read, then one should specify it explicitly. e.g. int n=128; // 128 byte field for planet name CString Planet; // read all 128 bytes and set CString to just the 0-terminated portion Planet.ReleaseBuffer(File.Read(Planet.GetBuffer(n),n)!=n?0:-1); Better yet, create structures that mirror the fixed length portions of the file and read them in entirety. e.g. struct Names { enum {NAMESIZE=128}; char Planet[NAMESIZE]; char System[NAMESIZE]; // etc. }; ABC MyNames; VERIFY(File.Read(&MyNames,sizeof(MyNames))==sizeof(MyNames)); and if you need it in a CString later: MyNames.Planet[NAMESIZE-1]=0; // make sure 0-terminated CString S=MyNames.Planet;

                  N Offline
                  N Offline
                  ns
                  wrote on last edited by
                  #8

                  Thanks! That gives me a lot to think about...:) Appreciate your help, ns

                  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