stefan
stefan

Reputation: 303

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();

Upvotes: 1

Views: 10301

Answers (2)

Arif_Khan
Arif_Khan

Reputation: 79

I found a different solution

myPrintMethod(CString stringtoprint, LPCWSTR myfile){
std::ofstream myfile;
myfile.open(filename, std::ofstream::out);
myfile << CT2A(stringtoprint);
myfile.close();

}

Upvotes: 1

stefan
stefan

Reputation: 303

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();
}

Upvotes: 1

Related Questions