Problem writing listbox strings to a text file in C++; File contents are numbers instead of strings

Posted on 2011-10-24
Last Modified: 2012-05-12
Easiest thing in the world: writing strings to a text file that are fetched from a listbox in C++ 6.0(or so I thought).

Here's my code:

#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;"C:\MyFile.txt");

m_listBox.GetText(0, strMyString);
myfile << strMyString << endl;

The file gets created, and is written to.  But when I open the file in windows explorer, instead of seeing the a string in the file, I see a number that looks like a memory address, such as 0034544C.

If I alter the code above, to write string like this:

myfile << "My Test String" << endl;

then the string gets written correctly.

So, the issue is perhaps with the call to the list box's GetText() function.  However, in the debugger, the string variable I pass into the call to GetText() shows a valid string (in both the Watch window and when I mouse hover over the variable).

Any thoughts?

Thanks very much.
Question by:cjm20
    LVL 86

    Accepted Solution

    You are getting the text from yopur list box as a CString, which is not really meaningful for an ofstream - use 'CString::operator LPCTSTR()' ( to get a string, e.g.
    m_listBox.GetText(0, strMyString);
    myfile << (LPCTSTR) strMyString << endl;

    Open in new window

    LVL 86

    Expert Comment

    BTW, if your project is set to UNICODE, you need to either use a 'std::wofstream' instead or convert the list box data to ASCII if you need that character format, e.g.
    m_listBox.GetText(0, strMyString);
    char* buf = new char[strMyString.GetLength() + 1];
    wcstombs(buf,LPCTSTR) strMyString,trMyString.GetLength() + 1);
    myfile << buf << endl;
    delete[] buf;

    Open in new window


    Author Closing Comment

    Perfection.  Simple matter of casting.  Thanks a bunch.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now