C++ 使用数字删除文件 io 行

C++ Deleting a File io line using a number

本文关键字:文件 io 删除 数字 C++      更新时间:2023-10-16

我现在使用文件 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;
}