wofstream automatically narrows wide characters

65 views Asked by At
#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;
}

This code is a simple "encrypter" that plays with altcodes to conceal characters. Though, when the application is done, the output file will be empty. The file output file I specified isn't anywhere at all. This application is meant to be run from shell. So, say I want to encrypt dummy.txt, I'll have to use this:RKRIPT dummy.txt out.txt. At first I thought that I was using the stream incorrectly, leading to characters not to be printed out. But after changing

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

to this(notice the changes from WCHAR_MAX to CHAR_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;
    }

My output was just fine, because there were only narrow characters(ASCII) to write on my file. Though, I don't know how to fix this, how do I get my WIDE characters to be written onto a file using a WIDE stream? Thanks for any reply.

0

There are 0 answers