Solved

How can I read a text file into a CString?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

10 Experts available now in Live!

Get 1:1 Help Now