943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1727
  • C++ RSS
Oct 21st, 2007
0

File read problem

Expand Post »
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,
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
pri_skit is offline Offline
19 posts
since Dec 2006
Oct 21st, 2007
0

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.
C++ Syntax (Toggle Plain Text)
  1. FileName += "\n";
  2. f.write(FileName.GetBuffer(), FileName.GetLength());

or
C++ Syntax (Toggle Plain Text)
  1. FileName += "\n";
  2. 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)
  1. char buffer;
  2. while( f.read(&buffer, 1) == 1 && buffer != '\n')
  3. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 21st, 2007
0

Re: File read problem

Use >> operator rather.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 21st, 2007
0

Re: File read problem

Click to Expand / Collapse  Quote originally posted by ithelp ...
Use >> operator rather.
CFile doesn't support that, which is one reason I hate that class.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 21st, 2007
0

Re: File read problem

CFile doesn't support that, which is one reason I hate that class.
Thanks for your help.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
pri_skit is offline Offline
19 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading data from file to vector
Next Thread in C++ Forum Timeline: C++ Random Numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC