使用getline()从文件中发出读取行

时间:2014-11-24 04:41:06

标签: c++ fstream ifstream getline

我设置了readFile()方法,以便我的'myList'向量保持更新,但是当我运行程序时,尽管我的addItem()方法正确地写入文件,但我的向量是空的。

我一直在寻找有关ifstream和getline的问题,但大多数解决方案相当于预先知道每行读取的项目数量的解决方法。

如果有人能告诉我我做错了什么,我会很感激。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

void readFile(vector<string> myList);
void writeToFile(vector<string> myList);
void mainMenu();
void displayList(vector<string> myList);
void addItem(vector<string> myList);
void removeItem(vector<string> myList);

string fileName = "C:\\Users\\Owner\\Documents.todo_list.txt";

int main()
{
    vector<string> myList;

    int choice;

    do
    {
        readFile(myList);

        mainMenu();

        cin >> choice;

        cout << endl;

        switch(choice)
        {
            case 1:
            {
                displayList(myList);
                break;
            }
            case 2:
            {
                addItem(myList);
                break;
            }
            case 3:
            {
                removeItem(myList);
                break;
            }
            default:
            {

            }
        }

        cout << endl;
    } while (choice != 0);

    return 0;
}

void readFile(vector<string> myList)
{
    ifstream read(fileName.c_str(), ios::in);

    if(!read)
    {
        cout << "Could not open file." << endl;
        exit(1);
    }

    if (myList.size() > 0)
    {
        myList.clear();
    }
    else
    {
        string item;

        getline(read,item);

        myList.push_back(item);
    }

    read.close();
}

void writeToFile(vector<string> myList)
{
    ofstream rewrite(fileName.c_str(), ios::out);

    for (int i = 0; i < myList.size(); ++i)
    {
        rewrite << myList[i] << endl;
    }

    rewrite.close();
}

void mainMenu()
{
    cout << " ========== Main Menu ==========" << endl;
    cout << " 1. Display To-do List" << endl;
    cout << " 2. Add item to the To-do List" << endl;
    cout << " 3. Remove item from the To-do List" << endl;
    cout << "\n 0. Exit program" << endl;
    cout << ": ";
}

void displayList(vector<string> myList)
{
    cout << " ========== To-do List =========" << endl;

    if (myList.empty())
    {
        cout << "      ----- empty list -----" << endl;
    }
    else
    {
        for (int i = 0; i < myList.size(); ++i)
        {
            cout << " " << (i + 1) << ". " << myList[i] << endl;
        }
    }
}

void addItem(vector<string> myList)
{
    ofstream write(fileName.c_str(), ios::app);

    string item;

    cout << "Please enter an item to add to the list: ";

    cin.ignore();
    getline(cin, item);

    write << item << endl;

    write.close();
}

void removeItem(vector<string> myList)
{
    int item;

    displayList(myList);

    cout << "\nPlease select an item to delete: ";
    cin >> item;

    cout << endl;
    cout << "...removing \"" << myList[item - 1] << "\"" << endl;

    myList.erase(myList.begin() + (item - 1));

    writeToFile(myList);
}

1 个答案:

答案 0 :(得分:2)

readFile的参数似乎是一个参考:

void readFile(vector<string>& myList)
BTW,当前的readFile只读取一行。