?
Solved

How can I read a text file into a CString?

Posted on 2004-09-22
2
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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