Solved

How can I read a text file into a CString?

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 71
How to determine file rename from EventLog ? 2 58
countEvens challenge 2 115
Problem to Office 1 44
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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

733 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