File read problem

Reply

Join Date: Dec 2006
Posts: 18
Reputation: pri_skit is an unknown quantity at this point 
Solved Threads: 1
pri_skit pri_skit is offline Offline
Newbie Poster

File read problem

 
0
  #1
Oct 21st, 2007
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,
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File read problem

 
0
  #2
Oct 21st, 2007
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.
  1. FileName += "\n";
  2. f.write(FileName.GetBuffer(), FileName.GetLength());

or
  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
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,777
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 113
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: File read problem

 
0
  #3
Oct 21st, 2007
Use >> operator rather.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File read problem

 
0
  #4
Oct 21st, 2007
Originally Posted by ithelp View Post
Use >> operator rather.
CFile doesn't support that, which is one reason I hate that class.
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 18
Reputation: pri_skit is an unknown quantity at this point 
Solved Threads: 1
pri_skit pri_skit is offline Offline
Newbie Poster

Re: File read problem

 
0
  #5
Oct 21st, 2007
Originally Posted by Ancient Dragon View Post
CFile doesn't support that, which is one reason I hate that class.
Thanks for your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC