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

时间:2014-10-13 06:42:21

标签: c++ class header fstream cstring

我已经困惑了几个小时,我应该如何解决这个问题。我的课程实现文件中有两个函数。无论如何,我一直试图让我的程序在我的计算机上的这个特定文件(这是一个包含许多单词的字典列表)中搜索单词,这些文件到目前为止已经成功。

但是,问题在于我试图将所有混乱版本的文字带到一个新创建的文本文档中来存储它们......到目前为止,它还没有工作,我&# 39;我真的很困惑。我在代码中出现了问题。

请注意:我是一名正在学习C ++的学生,代码有点无组织,直到我完成该程序的一般核心。我的导师说我不允许使用字符串类,仅限cstring。

需要帮助的区域:

" cout<< tmpword<< ENDL; //我想写入我的新文本文件****帮助" 和 "&new_dictionary LT;

提前致谢!

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;

}

1 个答案:

答案 0 :(得分:0)

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