| | |
File read problem
![]() |
•
•
Join Date: Dec 2006
Posts: 18
Reputation:
Solved Threads: 1
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,
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,
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.
or
Then I would use a C character array to read it back into memory and assign the CString object to it
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.
C++ Syntax (Toggle Plain Text)
FileName += "\n"; f.write(FileName.GetBuffer(), FileName.GetLength());
or
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Oct 21st, 2007 at 6:57 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Use >> operator rather.
![]() |
Similar Threads
- "Cannot delete file: Cannot read from the source file or disk." (Windows NT / 2000 / XP)
- Read/write to same file > once, Help (C++)
- read data from file (C++)
- php.ini file not being read (PHP)
- problem in reading a file (Shell Scripting)
- Question about file read/write (Java)
- File processing problem (C++)
- Browser hijacked by About:Blank (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: reading data from file to vector
- Next Thread: C++ Random Numbers
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






