简体   繁体   English

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

[英]Issue reading line from file with getline()

I have my readFile() method set up so that my 'myList' vector stays updated, but when I run the program my vector is empty despite my addItem() method properly writing to the file. 我设置了readFile()方法,以便我的“ myList”向量保持更新,但是当我运行程序时,尽管我的addItem()方法正确写入了文件,但向量还是空的。

I've tried looking around for issues regarding ifstream and getline but most of the solutions amount to workarounds where the number of items being read per line is known in advance. 我曾尝试查找有关ifstream和getline的问题,但大多数解决方案都属于解决方法,其中预先知道每行读取的项目数。

If someone can tell me what I'm doing wrong I'd appreciate it. 如果有人可以告诉我我在做什么错,我将不胜感激。

#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);
}

The parameter of the readFile seems to be a reference: readFile的参数似乎是一个参考:

void readFile(vector<string>& myList)

BTW, the current readFile just reads a single line. 顺便说一句,当前的readFile只读取一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM