Вопрос

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?

Это было полезно?

Решение

You need to use fwrite() or the iostream equivalent to write binary data. fprintf() and eqivalents are for text (as in readable text, not binary).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top