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

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

本文关键字:文本 文件 C++ 帮助 创建 于类 实现      更新时间:2023-10-16

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

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

请注意:我是一名目前正在学习C++的学生,在我完成程序的一般核心之前,代码有点杂乱无章。我的教练说我不允许使用字符串类,ONLY cstring

需要帮助的领域:

"cout<<tmpword<<endl;//我想要写入新文本文件的内容***帮助"和"新建分区<

提前感谢!

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:ttt      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);