visual-c++file-iocstring

Write CString to file


I am trying to write a cstring to a file, but have so far been unsuccessfull. I have tried the following:

std::ofstream myfile;
myfile.open(Placering, std::ofstream::out);
myfile << output;
myfile.close();

This however just seems to print the address of "output" to "myfile".

I then tried

for(int i = 0; i < output.getlength(); i++){
    myfile << output[i]
}

for every element in output, but that seems to just print the ASCII value of the characters to "myfile".

How do i correctly write my CString to a file? The content of the CString file can be HTML and rtf code.

EDIT: I found a solution, by casting the CString to a CStringA

std::ofstream myfile;
CStringA output = T2A(stringtoprint);
myfile.open(filename, std::ofstream::out);
for(int i = 0; i < output.GetLength(); i++){
    myfile << output[i];
}
myfile.close();

Solution

  • I found a solution, by casting the CString to a CStringA

    myPrintMethod(CString stringtoprint, LPCWSTR myfile){
        std::ofstream myfile;
        CStringA output = T2A(stringtoprint);
        myfile.open(filename, std::ofstream::out);
        for(int i = 0; i < output.GetLength(); i++){
            myfile << output[i];
        }
        myfile.close();
    }