将CString写入文件



我正在尝试将cstring写入文件,但到目前为止没有成功。我尝试过以下几种:

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

然而,这似乎只是将"输出"的地址打印到"myfile"。

然后我尝试了

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

对于输出中的每个元素,但这似乎只是将字符的ASCII值打印到"myfile"。

如何正确地将CString写入文件?CString文件的内容可以是HTML和rtf代码。

编辑:我找到了一个解决方案,通过将CString转换为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();

我找到了一个解决方案,通过将CString转换为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();
}

我发现了一个不同的解决方案

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

}

最新更新