Solved

How can I read a text file into a CString?

Posted on 2004-09-22
2
283 Views
Last Modified: 2013-11-20
Hello,

I have a text file that I am trying to read into a CString and then write the contents to another file.  It works well except it cut's off a few lines at the end of the original file.  I should mention that the original file is 1060 Kb in size and the copied file is 1058 Kb.  I have tried two different ways of doing it.  Both methods are shown below.  I would appreciate if anyone can help me out. Thx.

/*************METHOD 1*************************
CStdioFile theFile(_T("C:\\Test.txt"), CFile::modeRead | CFile::typeText);
CString origString;
CString finalString;
while(theFile.ReadString(origString))
{
      finalString += origString + " ";
}

ofstream saveTo("C:\\TestCopy.txt");
SaveTo << string(finalString);

/*************METHOD 2*************************

CString finalString = "";
string fileLine = "";
fstream file_stream("C:\\Test.txt", ios::in);
while(!file_stream.eof())
{
      file_stream >> fileLine;
      finalString += fileLine + " ";
}

ofstream saveTo("C:\\TestCopy.txt");
saveTo << string(finalString);


Thanks!
0
Comment
Question by:StanChart
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 12123705
don't use CFile::typeText, it may cut some carriage return/newline character, you must handle your file as binary to obtain an exact copy. Also don't use STL string to acomplish this. Just use another CFile or CStdioFile object as output.
0
 

Author Comment

by:StanChart
ID: 12123742
You're the man, thanks!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question