File read problem
-
I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,
priyank
-
I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,
priyank
-
I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,
priyank
Try the following to write: CString StringToWrite; StringToWrite="ABC";//e.g. f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1); Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); str+=B; if(B=='\0')break; } Not the best way to do it, but if it work U R OK!
kostas KEL
-
Try the following to write: CString StringToWrite; StringToWrite="ABC";//e.g. f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1); Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); str+=B; if(B=='\0')break; } Not the best way to do it, but if it work U R OK!
kostas KEL
KEL3 wrote:
f.Write (StringToWrite.GetBuffer(20),StringToWrite.GetLength()+1);
Why are you unnecessarily calling
GetBuffer()
?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I have written a CString Object into Text file by using : f.Write (&FileName,FileName.GetLength());//FileName is CString Object Now I want to Read CString object back How this could be done?I am using code : LPTSTR p = CurrentLanguageName.GetBuffer(11); void *g=(void *)p; f.Read (g,CurrentLanguageFileLen); CurrentLanguageName.ReleaseBuffer( ); //But this method don't work. Regards,
priyank
pri_skit wrote:
f.Write (&FileName,FileName.GetLength());//FileName is CString Object
You might want to check the contents of the file you are writing to, as you are writing the address of the
CString
object to the file.pri_skit wrote:
//But this method don't work.
Why are you bothering with a
void
pointer?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
pri_skit wrote:
f.Write (&FileName,FileName.GetLength());//FileName is CString Object
You might want to check the contents of the file you are writing to, as you are writing the address of the
CString
object to the file.pri_skit wrote:
//But this method don't work.
Why are you bothering with a
void
pointer?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote: Why are you unnecessarily calling GetBuffer()? If you write:
f.Write (&FileName,FileName.GetLength());
you are about to write the whole object into the file (including private variables etc...) and that is generally BAD. In fact the correct code for that would be:f.Write (&FileName,sizeof(FileName));
since the previous code would probably crash your app if the string buffer (allocated on the heap) is longer than the size of the object. =============================================== A little correction: Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); if(B=='\0')break; // Before "str+=B;" to avoid adding two '\0' at the end. str+=B; }kostas KEL
-
DavidCrow wrote: Why are you unnecessarily calling GetBuffer()? If you write:
f.Write (&FileName,FileName.GetLength());
you are about to write the whole object into the file (including private variables etc...) and that is generally BAD. In fact the correct code for that would be:f.Write (&FileName,sizeof(FileName));
since the previous code would probably crash your app if the string buffer (allocated on the heap) is longer than the size of the object. =============================================== A little correction: Try the following to read: CString str; char B; for(i=0;i<1000;i++){ f.Read(&B,sizeof(B)); if(B=='\0')break; // Before "str+=B;" to avoid adding two '\0' at the end. str+=B; }kostas KEL
KEL3 wrote:
In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName));
Wrong. This would still write the address of
FileName
to the file. Why is it so hard to code something like:f.Write(FileName, FileName.GetLength());
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
KEL3 wrote:
In fact the correct code for that would be: f.Write (&FileName,sizeof(FileName));
Wrong. This would still write the address of
FileName
to the file. Why is it so hard to code something like:f.Write(FileName, FileName.GetLength());
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Yes, sorry. I meant that,
f.Write (&FileName,sizeof(FileName));
would be the correct code to save the object in the file, not the string that it has allocated. I would be more clear in future! But the code I posted above:f.Write(FileName.GetBuffer(20), FileName.GetLength()+1);
works. I have done it before (that's why I decided to answer, despite the fact I'm not an expert). Your piece of code does the same with less code! I just didn't have in mind that CString could be so easily type-casted into LPCSTR and then tovoid*
. But you must also use the+1
, so that the '\0' character be written. This is needed if you don't know the size of the string from the beginning. No mean to argue, you are right. Thanks for helping me in the past too. Remember this: http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=2117813&df=100&fr=15724.5#xx2117813xx[^]kostas KEL