我有一个简单的“加密”函数,它与wstring变量一起工作,我希望使用wofstream将该函数的结果写入文件中。
这是我的密码:
void save_file() {
wstring text = L"Text to be encrypted. The text is encrypted but not saved";
wstring enctext = encrypt(text);
wprintf_s(L"%s\n", enctext);
wofstream output_stream;
output_stream.open(L"myfile.enc");
output_stream<< enctext ;
output_stream.close();
}
wstring encrypt(wstring decrypted) {
wstring encrypted;
for (unsigned int i=0; i<decrypted.length(); i++) {
encrypted += wchar_t(int(decrypted[i]) + 128);
}
return encrypted;
}所以这件事的问题..。代码是,尽管wprintf_s函数输出整个加密文本,但在编写的文件中,我只看到ASCII范围内的字符(或者至少在我看来是这样)。加密文本保存到找到一个未知字符(由显示在控制台中)。我想保存任何字符,我希望它们保存为宽字符(每个字符一个字)。我该怎么做?
发布于 2013-05-27 15:58:15
您需要使用fwrite()或相当于写入二进制数据的iostream。fprintf()和eqivalents用于文本(如可读的文本,而不是二进制文本)。
https://stackoverflow.com/questions/16776281
复制相似问题