【发布时间】:2013-12-09 19:55:09
【问题描述】:
我正在尝试将 cstring 写入文件,但到目前为止还没有成功。 我尝试了以下方法:
std::ofstream myfile;
myfile.open(Placering, std::ofstream::out);
myfile << output;
myfile.close();
然而,这似乎只是将“输出”的地址打印到“我的文件”。
然后我尝试了
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();
【问题讨论】:
-
如果这是 UNCODE 版本,请look at this answer 了解 BOM。
-
恐怕我不太了解您链接到的答案中发生了什么。我对 C++ 很陌生。
-
如果您正在构建一个 Unicode 版本(看起来很可能),那么
CString代表一个wchar_t的数组,而不是一个char的数组。在这种情况下,您需要将其写入wofstream,而不是ofstream。 -
感谢您的帮助。我找到了另一种解决方案,我在原始帖子中对此进行了描述。
标签: visual-c++ file-io cstring