繁体   English   中英

有关类实现和写入创建的文本文件的C ++帮助

[英]C++ help regarding class implementation and writing into a created text file

关于如何解决此问题,我已经困惑了几个小时。我的类的实现文件中有两个函数。 无论如何,我一直在尝试让我的程序对我计算机上的某个文件(这是一个包含多个单词的词典列表)中的单词进行加扰,到目前为止,该程序已经成功了。

但是,问题在于我正试图将所有单词的加扰版本带到一个新的存储它们的文本文档中...到目前为止,它还没有起作用,我真的很困惑。 我在代码中加粗了问题所在。

请注意:我是一名正在学习C ++的学生,在我完成程序的总体核心之前,代码有些杂乱无章。 我的老师说我不允许使用字符串类, 只能使用cstring。

需要帮助的领域:

“ cout << tmpword << endl; //我要写入新文本文件的内容**** HELP”和“ new_dictionary <

提前致谢!

void Scrambler::scrambleletters_in_file()
{
    char * tmpword = nullptr;  //NULL

    // compare with every single word in the dictionary
    for (int i = 0; i < dictionarySize; i++)
    {
        // clean the space holding the previous word if any
        if (tmpword != nullptr)
            delete[] tmpword;

        //prepare space
        tmpword = new char[strlen(dictionary[i]) + 1];
        strcpy_s(tmpword, strlen(dictionary[i]) + 1, dictionary[i]);

// do the unscrambling stuff here

        // 1 2 3 4 5 6  7
        int first = 1; // H I C K E N '/0'
        int last = strlen(tmpword) - 2; // = C H I C K E 

        // We are showing the difference here, where the first/last letters are
        // not scrambled.

        for (int i = first; i < last; i++)
        {

            int j = rand() % last + 1; // Chicken[1-5]'s letters will be randomized
            // For instance: "hicke" will be scrambled.

            char result = tmpword[i];
            tmpword[i] = tmpword[j];
            tmpword[j] = result;
        }

        cout << tmpword << endl; //What I want written into my new text file **** HELP

    }


}

void Scrambler::createNew_dictionary()
{

    // ofstream is used for writing files
    // We'll make a file called scrambled_words.txt
    char newfilename[25];

    char choice3;

    cout << "\nHow do you want to create the new scrambled file? (Input 1 or 2) " << endl;
    cout << " - PRESET: (\"unscrambled_file.txt\"):  1 " << endl;
    cout << " - CUSTOM:\t\t\t      2 " << endl;
    cout << "\nChoice ===> ";
    cin >> choice3;


    if (choice3 == '1')
    {

        strcpy_s(newfilename, 25, "unscrambled_file.txt"); // copies the preset filename into "newfilename"

    }

    if (choice3 == '2')
    {

        cout << "\nWhat would you like the new file name to be? (EXAMPLE: \"test.txt\")" << endl;
        cout << "~ Note: if you type in an already existing file name, it will be overwritten " << endl;
        cout << "* WARNING: USE underscores INSTEAD of spaces in the text file name! " << endl; // Since I'm supposed to use cstring
        // instead of string, this won't work with spaces
        cout << "\nFile Name ====> ";
        cin >> newfilename; // ^ Practing by adding user input customization ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

//*TO DO (and practice) - SHOW A LIST OF CURRENTLY EXISTING .txt FILES, AND SELECT A FILE TO DELETE//
    }

    ofstream new_dictionary(newfilename); // Opens and/or creates the specified file if it hasn't already.

    // If we couldn't open the output file stream for writing
    if (!new_dictionary)
    {
        // Print an error and exit
        cout << "Uh oh, the text file could not be opened for writing!" << endl;
        system("PAUSE"); // this is just included so you have time to read what the error message
                         // says before the program is exited by the next statement.

        exit(1); // equivilent to "return 0;" in main function
    }

    else
        cout << "File successfully created and opened" << endl;

    new_dictionary<<scrambleletters_in_file(); // <--- MY PROBLEM ********

    /* We'll write two lines into this file
    new_dictionary << "This is line 1" << endl;
    new_dictionary << "This is line 2" << endl;
    */

    cout << "File successfully overwritten" << endl;

    new_dictionary.close();
    cout << "File closed. " << endl;

}

scrambleletters_in_file()您正在将字符串输出到标准输出的cout (默认为控制台)。 这行:

new_dictionary<<scrambleletters_in_file();

表示“计算函数scrambleletters_in_file的结果并将其写入new_dictionary ”。 其结果是void ,因此代码甚至无法编译。

您需要将流传递给函数:

void Scrambler::scrambleletters_in_file(ostream& out) {
    ...
    out << tmpword << endl;
}

并这样称呼它:

scrambleletters_in_file(new_dictionary);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM