C++ 使用数字删除文件 io 行
C++ Deleting a File io line using a number
我现在使用文件 io 创建一个待办事项列表,我添加到列表中的每个任务的开头都附加了一个序列号,我试图完成的是如何使用该序列号作为删除文件中整行文本的一种方式。我知道删除一行需要一个临时文件并重命名它(至少从我阅读的内容来看(,但我无法让它删除我想要的行,或者它只会删除序列号。
例如,如果我想删除第二行,我会输入"- 2",它会这样做。这是我到目前为止的代码。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
// Class for our add, delete, and view functions.
class Todo {
public:
void add (string);
void del (string);
void view();
};
int main () {
Todo l;
string item;
char command;
lPlaceholder:
cout << "ttYour ToDo List" << endl;
cout << "Press + then enter your item to add to the list" << endl;
cout << "Press - then enter your items serial number to remove" << endl;
cout << "Press ? to display all items in your list" << endl;
cout << "Press x to quit the program" << endl;
cin >> command;
if (command == '+' || command == '-') {
getline(cin, item);
}
// Simple menu
switch(command){
case '+': l.add(item); goto lPlaceholder;
case '-': l.del(item); goto lPlaceholder;
case '?': l.view(); goto lPlaceholder;
case 'x': exit (0);
default: cout << "Invalid choice.n";
}
}
// Add function
void Todo::add(string item)
{
char command;
string info;
int serial = 1;
ifstream in;
in.open ("ToDo List.txt");
while (getline(in, info))
serial ++;
time_t rawtime;
struct tm*timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
ofstream f;
f.open ("ToDo List.txt", ios::app);
f<< serial <<" "<< item << " " << timeinfo->tm_mon << " " << timeinfo->tm_mday << " " << (timeinfo->tm_year + 1900) << endl;
f.close();
cout << endl;
cout << "New item added to the list";
cout << endl;
cout << endl;
}
// Delete function
void Todo::del(string item)
{
string info;
ifstream myfile("ToDo List.txt");
ofstream newfile("temp.txt");
if (myfile.is_open()) {
while (getline (myfile, item)) {
n = item;
if (n.find(item)!=std::string::npos) {
newfile<<n;
}
}
myfile.close();
remove("ToDo List.txt")
rename("temp.txt", "ToDo List.txt");
}
}
void Todo::view()
{
}
要删除项目,只需逐行读取输入文件,将每一行写入新的输出文件,忽略包含要"删除"的序列号的行。 您的商品以序列号开头,因此很容易找到。
尝试这样的事情:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <stdlib.h>
using namespace std;
// Class for our add, delete, and view functions.
class Todo {
public:
void add (string);
void del (int);
void view();
};
int main () {
Todo l;
string item;
char command;
int serial;
do
{
cout << "ttYour ToDo List" << endl;
cout << "Press + then enter an item to add to the list" << endl;
cout << "Press - then enter an item serial number to remove" << endl;
cout << "Press ? to display all items in the list" << endl;
cout << "Press x to quit the program" << endl;
cin >> command;
if (command == '+' || command == '-') {
getline(cin, item);
}
// Simple menu
switch (command)
{
case '+':
l.add(item);
break;
case '-': {
istringstream(item) >> serial;
l.del(serial);
break;
}
case '?':
l.view();
break;
case 'x':
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
while (true);
return 0;
}
// Add function
void Todo::add(string item)
{
string info;
int next_serial = 1;
int serial;
ifstream in("ToDo List.txt");
while (getline(in, info))
{
istringstream(info) >> serial;
if (serial >= next_serial)
next_serial = serial + 1;
}
in.close();
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
ofstream f("ToDo List.txt", ios::app);
f << next_serial << " " << item << " " << timeinfo->tm_mon << " " << timeinfo->tm_mday << " " << (timeinfo->tm_year + 1900) << endl;
f.close();
cout << endl;
cout << "New item added to the list";
cout << endl;
cout << endl;
}
// Delete function
void Todo::del(int item)
{
ifstream myfile("ToDo List.txt");
if (!myfile.is_open()) return;
ofstream newfile("temp.txt");
if (!newfile.is_open()) return;
string info;
int serial;
bool deleted = false;
while (getline (myfile, info))
{
if (!deleted)
{
istringstream(info) >> serial;
if (serial == item)
{
deleted = true;
continue;
}
}
newfile << info << endl;
}
if (deleted)
{
myfile.close();
newfile.close();
remove("ToDo List.txt")
rename("temp.txt", "ToDo List.txt");
cout << endl;
cout << "Item removed from the list";
cout << endl;
cout << endl;
}
else
{
cout << endl;
cout << "Item not found in the list";
cout << endl;
cout << endl;
}
}
void Todo::view()
{
ifstream myfile("ToDo List.txt");
string info;
while (getline (myfile, info))
cout << info << endl;
}
相关文章:
- 为什么在读取文件大小时文件IO速度会发生变化
- C++中真正的异步文件 IO
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- 如何使用C 文件IO修复程序
- C++ 使用数字删除文件 io 行
- 我们是否需要互斥锁来执行多线程文件 IO
- C++带有字符串和浮点的文件 IO
- 多线程文件 IO 程序在线程数增加时行为不可预测
- 在C++程序中,IO/文件 IO 操作的优先级是否高于 CPU 操作
- C++ 中的文件 IO 出错。在抛出 'std::length_error' 的实例后调用的终止 what(): basic_string::调整大小
- 文件IO循环错误
- 如何确保数据刷新到文件 IO (WriteFile()) 上的 HDD(未缓冲)
- Win32 文件 IO 工作,但打开 et.失败.这是怎么回事
- C++ 中的详细文件 IO 错误报告
- c++中的压缩文件IO
- 提供带有libjpeg的自定义文件IO
- 在C++文件IO期间捕获所有错误(或ios对象中的任何错误)的最佳实践
- 带有tmpnam的C++文件IO永久临时文件
- 文件IO中的可移植性问题
- 使用/dev/random的文件io花费的时间太长