Solved

How can I read a text file into a CString?

Posted on 2004-09-22
2
281 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now