ofstream.put无法正常工作

时间:2014-10-06 13:50:08

标签: c++

我正在完成一项学校任务并最终遇到麻烦。 因此该程序背后的想法是对txt文件进行decypher,但是当尝试打印到不同的文本文件时,它根本就不打印,即使它使用的代码与其他能够使用的函数完全相同。打印。

这里是代码: (并非所有相关内容都包含在内,因为它还包含较小分配的代码) (另外,有些评论是荷兰语,对不起!)

#include <cassert>
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;


string output;
string input;

char rotate(char a, int r, bool e)
{
    //precondition:
    assert(r>=0);
    //postcondition: we hebben a ge-encrypt of ge-decrypt
    int b;
    if (a<32)
    {
        b=a;
        return b;
    }
    if (32<=a<=128)
    {
        if (e)
        {
            b = (a-32 + (r% (128-32))+(128-32))%(128-32)+32;
            return b;
        }
        else
        {
            b = (a-32 - (r% (128-32))+(128-32))%(128-32)+32;
            return b;
        }
    }

}

void test_rotate()
{
    //precondition
    assert(true);
    //postcondition: we printen het origineel, de ge-encrypte versie, en de inverse van de ge-encrypte versie
    cout << "Wat is de character die je wilt encrypten \n";
    char a;
    cin >> a;
    cout << "\nVoer een willekeurig natuurlijk getal in\n";
    int r;
    while (true)
    {
        cin >> r;
        cout << a << " " << rotate(a, r, true) << " " << rotate(rotate(a, r, true), r, false) << endl;
    }
}

bool open_input_and_output_file (ifstream& in_stream, ofstream& out_stream)
{
    //precondition:
    assert(true);
    //postcondition: vraag de namen van de files, open ze en geef aan of dat is gelukt of niet.
    string in_file_name;
    string out_file_name;
    cout << "geef de naam die van het te encrypten/decrypten bestand\n";
    cin >> in_file_name;
    cout << "geef de naam van het bestand waarnaar de encryptie/decryptie geschreven moet worden\n";
    cin >> out_file_name;

    in_stream.open(in_file_name.c_str());
    out_stream.open(out_file_name.c_str());
    if (in_stream.fail() || out_stream.fail() || (in_file_name==out_file_name))
    {
        cout << "de bestanden kunnen niet geopend worden of zijn hetzelfde bestand";
        return false;
    }
    return true;
}


void OTP(ifstream& in_stream, ofstream& out_stream)
{
    //precondition:
    assert(true);
    /*postcondition:
    Vraag of ge-encrypt of gedecrypt moet worden,
    open de files waarvoor dit moet gebeuren,
    vraag voor de sleutel,
    encrypt/decrypt de file.
    */
    cout << "Wilt u 0: decrypten of 1: encrypten?\n";
    bool crypten;
    cin >> crypten;
    open_input_and_output_file(in_stream, out_stream);
    cout << "Geef de sleutel voor het encrypten/decrypten\n";
    unsigned int key;
    cin >> key;
    char character;
    srand(key);
    in_stream.get(character);
    while(in_stream)
    {
        out_stream.put (rotate (character, rand(),crypten));
        in_stream.get(character);
        cout << "lolnope";
    }
}

void OTP_secret(ifstream& in_stream, ofstream& out_stream, int key)
{
    srand(key);
    char character;
    in_stream.get(character);
    int count=1;
    out_stream.open("source.txt");
    while(count <= 5 && in_stream)
    {
        out_stream.put (rotate(character, rand(), false));
        in_stream.get(character);
        count++;
    }
}

bool check_key(ifstream& in_stream, ofstream& out_stream)
{
    char character;
    in_stream.get(character);
    int count = 1;
    while (count <= 5 && in_stream)
    {
        if ((character <= 'z') && (character >= 'A') /*|| (character == ' ')*/)
        {
            in_stream.get(character);
            count++;
        }
        else
        {
            return false;
        }
    }
    return true;
}

int find_key(ifstream& in_stream, ofstream& out_stream)
{
    int count = 0;
    OTP_secret(in_stream,out_stream,0);
    while (!check_key(in_stream, out_stream))
    {
        OTP_secret(in_stream, out_stream, count);
        count++;
    }
    count--;
    return count;
}

void secret(ifstream& in_stream, ofstream& out_stream)
{

    in_stream.open("secret.txt");
    int key = find_key(in_stream, out_stream);
    char character;
    srand(key);
    in_stream.get(character);
    /*while(in_stream)
    {
        out_stream.put (rotate(character, rand(), false));
        cout << rotate(character,rand(),false);
        in_stream.get (character);
    }*/
    while(in_stream)
    {
        out_stream.put (rotate (character, rand(), false));
        cout << rotate (character, rand(), false);
        in_stream.get(character);
    }
    if (out_stream.is_open())
        cout << "\nout_stream is open\n";
    if (in_stream.is_open())
        cout << "in_stream is open\n";
}

int main()
{
    ifstream in_stream;
    ofstream out_stream;
    //test_rotate();
    //open_input_and_output_file(inputfile,outputfile);
    //OTP(in_stream,out_stream);
    secret(in_stream, out_stream);

    return 0;
} 

问题在于无效秘密,在最后的时间(in_stream)。 out_stream.put不是写入输出文件,而cout打印完全相同的东西将写入。 我在那里抛出了一些额外的支票,看看文件是否打开等。

我对问题的位置一无所知,因为使用void OTP_secret中的完全相同的代码可以正常工作,也可以使用void OTP。

1 个答案:

答案 0 :(得分:1)

out_stream.open(...);功能中没有secret,即您永远不会打开输出流。