• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

How do I output text to a text file?

I am trying to debug a program. In order to this I want to output some text contained in a CString variable to c:\debug.txt.

I already have the debug message I want in CString "csMessage".  How do I output the contents of  csMessage to c:\debug.txt.  

Code that I can use would be much appreciated!
  • 2
1 Solution
shaolinfunkAuthor Commented:
I forgot to add..c:\debug.txt does not exist yet.  How do I make it so that your function creates this file as well?  And if the file did already exist to just delete it and/or write over it?
Not a big problem - you can use an 'ofstream', which will overwrite the file when opening it, e.g.
#include <fstream>
#unsing namespace std;


ofstream os("c:\\debug.txt");

CString csMessage = "Test";

os << (LPCTSTR) csMessage << endl;


Open in new window

shaolinfunkAuthor Commented:

Works perfectly, thanks!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now