Another map question
-
The map is (long, char*). I get a char* buffer
tempB
from a CFile read, of sizenSize
. CAn I doaMap[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 -
The map is (long, char*). I get a char* buffer
tempB
from a CFile read, of sizenSize
. CAn I doaMap[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, nsYou can still use CString.
CString csWrite(_T("Hello World")); CFile cFile; cFile.Open(...); cFile.Write((void*)(LPCTSTR)csWrite, csWrite.GetLength());
-
You can still use CString.
CString csWrite(_T("Hello World")); CFile cFile; cFile.Open(...); cFile.Write((void*)(LPCTSTR)csWrite, csWrite.GetLength());
-
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
-
Can I use the same trick with Read?
CString abc; cFile.Read((void*)(LPCTSTR)abc, abc.GetLength());
Appreciate your help, nsint n=S.GetLength(); // this is icky S.ReleaseBuffer(File.Read(S.GetBuffer(n),n));
-
int n=S.GetLength(); // this is icky S.ReleaseBuffer(File.Read(S.GetBuffer(n),n));
-
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, nsArgh. 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;
-
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;