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.

Recommended Answers

All 4 Replies

you wrote it wrong -- what you wrote is the c++ class object, not the string contents. If the file is a normal text file where '\n' is line terminator then you need to tack on the '\n' to the end of the string then write only the string to the file.

FileName += "\n";
f.write(FileName.GetBuffer(), FileName.GetLength());


FileName += "\n";
f.write((LPCTSTR)FileName, FileName.GetLength());

Then I would use a C character array to read it back into memory and assign the CString object to it

char buffer;
while( f.read(&buffer, 1) == 1 && buffer != '\n')
   FileName += buffer;

Personally I hate the CFile class -- it is really crude and difficult to work with text files. A better solution is to use either standard c++ fstream if it is available with your compiler (and some compilers do not support it) or use CArchive in conjunction with CFile. CArchive will make the above code quite a bit easier and cleaner.

Use >> operator rather.

Use >> operator rather.

CFile doesn't support that, which is one reason I hate that class.

CFile doesn't support that, which is one reason I hate that class.

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.