C++ C++;试图从txt文件中删除使用过的行
我试图从一个txt文件(questions.txt)中删除使用过的行,并将它们放入另一个文件(usedQuestions.txt),但现在代码只是删除questions.txt,没有将任何内容放入temp.txt或重命名它;并且没有在usedQuestions.txt中输入任何内容 这是我的代码:C++ C++;试图从txt文件中删除使用过的行,c++,text-files,C++,Text Files,我试图从一个txt文件(questions.txt)中删除使用过的行,并将它们放入另一个文件(usedQuestions.txt),但现在代码只是删除questions.txt,没有将任何内容放入temp.txt或重命名它;并且没有在usedQuestions.txt中输入任何内容 这是我的代码: #include <iostream> #include <string> #include <fstream> using namespace std; int
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
//无限循环
对于(int i=1;i>0;i++){
//档案
ifstream txtFile(“questions.txt”);
流使用的问题(“usedQuestions.txt”);
流tempFile(“temp.txt”);
//字符串、整数等。
字符串问题;
字符串温度;
无符号整数lineNum=1;
int requestedLineNum;
//用户请求行
cin>>请求的LineNum;
cout我刚刚为您完成了代码编写,我将解释一下:
#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;
}
或
而不是
for (int i = 1; i > 0; i++) //it needs a variable (i)
3-我写了:
cout << "Write a line number (-1 for exit): ";
cin >> requestedLineNum;
在运行程序时轻松退出循环
5-我添加了
<< endl;
可能行号永远不等于requestedlinenum在测试时,您可能应该删除重命名()
和删除()
您从未关闭临时文件
。因此重命名(“temp.txt”,“questions.txt”);
可能不起作用。您复制文件的代码似乎在所选行之后复制了所有内容。代码很漂亮,但似乎不起作用。我得到了一个usedQuestions.txt文件,其中包含正确的问题,但我也得到了一个空的temp.txt文件,其中questions.txt被删除。我不明白temp.txt必须包含什么。我知道ght这是一个临时文件来移动questionsTrue。因此,在最后,它应该包含除已使用的问题之外的所有问题。事实并非如此。事实上,它是空的。那么temp.txt将与questions.txt相同?也许它是无用的…很抱歉这么晚了,它应该是相同的,但没有已使用的问题。
cout << "Write a line number (-1 for exit): ";
cin >> requestedLineNum;
if(requestedLineNum==-1) break;
else {...
<< endl;
//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++;