DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   File read problem (http://www.daniweb.com/forums/thread93765.html)

pri_skit Oct 21st, 2007 3:06 am
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,

Ancient Dragon Oct 21st, 2007 6:50 am
Re: File read problem
 
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());

or
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.

ithelp Oct 21st, 2007 7:33 am
Re: File read problem
 
Use >> operator rather.

Ancient Dragon Oct 21st, 2007 7:41 am
Re: File read problem
 
Quote:

Originally Posted by ithelp (Post 454979)
Use >> operator rather.

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

pri_skit Oct 21st, 2007 8:37 am
Re: File read problem
 
Quote:

Originally Posted by Ancient Dragon (Post 454986)
CFile doesn't support that, which is one reason I hate that class.

Thanks for your help.


All times are GMT -4. The time now is 11:09 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC