C ++使用数字

时间:2018-03-23 00:56:47

标签: c++ file

我现在正在使用文件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 << "\t\tYour 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()
{

}

1 个答案:

答案 0 :(得分:0)

要删除项目,只需逐行读取输入文件,将每行写入新输出文件,忽略包含要“删除”的序列号的行。您的商品以序列号开头,因此很容易找到。

尝试这样的事情:

#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 << "\t\tYour 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;
}