0

I have a simple "encryption" function that works with wstring variables and I want to write the result of this function into a file, using wofstream.

This is my code:

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

So, the problem with this piece of ... code is that although the wprintf_s function outputs the entire encrypted text, in the written file I only see the characters inside the ASCII range (or at least is what it seems to me). The encrypted text is saved until an unknown character is found (displayed by ? in the console). I want to save any character, and I want them saved as wide chars (1 word each one). How can I do this?

4

1 回答 1

1

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

于 2013-05-27T15:58:15.537 回答