将加密的wstring写入wofstream

Write encrypted wstring to wofstream

本文关键字:写入 wofstream wstring 加密      更新时间:2023-10-16

我有一个简单的"加密"函数,它与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"%sn", 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范围内的字符(或者至少是对我来说)。加密文本被保存,直到找到一个未知字符(由显示?)。我想保存任何字符,我希望他们保存为宽字符(1字每一个)。我该怎么做呢?

您需要使用fwrite()或等效的iostream来写入二进制数据。fprintf()及其等价值用于文本(即可读文本,而不是二进制文本)。