C ++试图从txt文件中删除使用过的行

时间:2018-11-20 20:58:57

标签: c++ text-files

我正在尝试从txt文件(questions.txt)中删除使用过的行,并将其放入另一个文件(usedQuestions.txt)中,但是现在代码只是删除了questions.txt,没有将任何内容置于临时位置.txt或重命名;而且不会在usedQuestions.txt中放置任何内容

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main () {

    // Loop infinitely
    for (int i = 1; i > 0; i++) {

        // Files
        ifstream txtFile("questions.txt");
        ofstream usedQuestions("usedQuestions.txt");
        ofstream tempFile("temp.txt");

        // Strings, ints etc.
        string question;
        string temp;
        unsigned int lineNum = 1;
        int requestedLineNum;

        // User requests line
        cin >> requestedLineNum;
        cout << "You have requested question number: " << requestedLineNum << endl;

        // Go through lines until the correct line is found
        while (getline(txtFile, question))
        {

            //If the line number is correct, output question
            if (lineNum == requestedLineNum)
            {
                cout << "Your question is: " << question << endl;

                // Write to temporary file, doesn't work?
                while (getline(txtFile, temp)) {
                    tempFile << temp;
                }

                // Write to file with used questions
                usedQuestions << question;
            }

            // Set up for while loop to go through lines
            lineNum++;
        }

        // Close and remove questions.txt, rename temp.txt to questions.txt
        txtFile.close();
        remove("questions.txt");
        rename("temp.txt", "questions.txt");
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我刚刚为您编写了代码,我将对其进行解释:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//This objects can be declared one time
ofstream usedQuestions("usedQuestions.txt");

// Strings, ints etc.
string question, temp;
unsigned int lineNum = 1;
int requestedLineNum;

int main () {

    // Loop infinitely
    while(true) {//Use while(true) or for(;;) for endless loops

        // Files
        ifstream txtFile("questions.txt");
        ofstream tempFile("temp.txt");
        lineNum = 1;

        // User requests line
        cout << "Write a line number (-1 for exit): ";//when you run the code what you think you have to do? It isn't written...
        cin >> requestedLineNum;

        //this is a possibility to exit from the main loop
        if(requestedLineNum==-1) break;
        else {
            cout << "You have requested question number: " << requestedLineNum << endl;

            // Go through lines until the correct line is found
            while(getline(txtFile, question))
            {//while it doesn't reach the end of the file read a line from the file

                //If the line number is correct, output question
                if (lineNum == requestedLineNum)
                {
                    cout << "Your question is: \"" << question << "\"\n" << endl;//You've forgotten the endl - your code generate one line with all questions!

                    // Write to file with used questions
                    usedQuestions << question << endl;
                }
                else tempFile << question << endl;//if it isn't the selected question step to the next line, so the question won't appear at the next iteration

                // Set up for while loop to go through lines
                lineNum++;
            }
            // Close and remove questions.txt, rename temp.txt to questions.txt
            txtFile.close();
            remove("questions.txt");
            rename("temp.txt", "questions.txt");
        }
    }
    return 0;
}

让我列出我的编辑内容:

1 -我已经从main函数中定义了变量,不必在每次循环时都声明它们。

2 -无限循环使用

 while(true) {}

for(;;) {}

代替

 for (int i = 1; i > 0; i++) //it needs a variable (i)

3 -我写过:

cout << "Write a line number (-1 for exit): ";
cin >> requestedLineNum;

因为谁将运行该程序,不知道他必须做什么

4 -我添加了:

 if(requestedLineNum==-1) break;
 else {...

在运行程序时轻松退出循环

5 -我添加了

 << endl;

因为当程序在生成一行所有问题之前先写入文件,因此在下一次迭代中不再起作用

6 -我替换了while循环,因为它与外部循环重复

//If the line number is correct, output question
if (lineNum == requestedLineNum)
{
    cout << "Your question is: " << question << endl;

    // Write to temporary file, doesn't work? //  <--
    while (getline(txtFile, temp)) {          //  <--
        tempFile << temp;                     //  <--
    }                                         //  <--

   // Write to file with used questions
   usedQuestions << question;
}

//If the line number is correct, output question
if (lineNum == requestedLineNum) {
     cout << "Your question is: \"" << question << "\"\n" << endl;

     // Write to file with used questions
     usedQuestions << question << endl;
}
    else tempFile << question << endl;

// Set up for while loop to go through lines
lineNum++;

现在:   1.读取文件questions.txt的每一行   2.如果是所选问题,请不要将其写入TempFile中,而应写入usedUsedions.txt中   3.如果不是选定的问题,则将其写在tempFile中(之后将是questions.txt)

对不起我的英语,我随时可以提供任何解释。