wofstream自动缩小宽字符

时间:2017-09-14 09:19:30

标签: c++ stream widechar

#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char * argv[]) {
    srand(time(NULL));
    if (argc < 1) {
        cout << "Too few arguments inserted.\nUSAGE: RKRIPT [InFile] [OutFile]" << endl;
        return EXIT_FAILURE;
    }
    string InFile, OutFile;
    InFile = argv[1];
    OutFile = argv[2];
    if (InFile.size() > FILENAME_MAX || OutFile.size() > FILENAME_MAX) {
        cout << "Invalid filename lenght.\nFILENAME_MAX = " << FILENAME_MAX;
        return EXIT_FAILURE;
    }
    wstring * Cont = new wstring;
    if (Cont == nullptr) {
        cout << "Memory allocation failed." << endl;
        return EXIT_FAILURE;
    }
    wifstream i_f;
    wchar_t temp;
    i_f.open(InFile);
    while (i_f.get(temp)) {
        *Cont += temp;
    }
    i_f.close();
    wofstream o_f;
    o_f.open(OutFile);
    long long int OutSize = 0;
    for (long long int i = 0; i < Cont->size(); i++) {
        do {
            temp = wchar_t(rand() % WCHAR_MAX); //Keeps getting another value for temp until its sum with the current character doesn't exceed WCHAR_MAX.
        } while (((long long int)temp + (long long int)Cont->at(i)) > WCHAR_MAX);
        o_f << temp + Cont->at(i) << temp;
        OutSize += 2;
    }
    o_f.close();
    cout << "Done. Input file was " << InFile << "(" << Cont->size() << " Bytes)" << ". Output file is " << OutFile << "(" << OutSize << " Bytes)";
    delete Cont;
    return EXIT_SUCCESS;
}

此代码是一个简单的&#34;加密器&#34;使用altcodes来隐藏角色。但是,当应用程序完成时,输出文件将为空。我指定的文件输出文件根本不在任何地方。此应用程序旨在从shell运行。所以,我想要加密dummy.txt,我必须使用它:RKRIPT dummy.txt out.txt。 起初我以为我错误地使用了流,导致字符无法打印出来。但改变之后

for (long long int i = 0; i < Cont->size(); i++) {
        do {
            temp = wchar_t(rand() % WCHAR_MAX); //Keeps getting another value for temp until its sum with the current character doesn't exceed WCHAR_MAX.
        } while (((long long int)temp + (long long int)Cont->at(i)) > WCHAR_MAX);
        o_f << temp + Cont->at(i) << temp;
        OutSize += 2;
    }

到此(请注意从WCHAR_MAXCHAR_MAX的更改)...

for (long long int i = 0; i < Cont->size(); i++) {
        do {
            temp = wchar_t(rand() % WCHAR_MAX); //Keeps getting another value for temp until its sum with the current character doesn't exceed CHAR_MAX.
        } while (((long long int)temp + (long long int)Cont->at(i)) > CHAR_MAX);
        o_f << temp + Cont->at(i) << temp;
        OutSize += 2;
    }

我的输出很好,因为我的文件只能写入窄字符(ASCII)。虽然,我不知道如何解决这个问题,但如何使用WIDE流将我的WIDE字符写入文件?谢谢你的回复。

0 个答案:

没有答案