2014-11-24 73 views
0

我有我的readFile()方法設置,以便我的myList的向量保持更新,但是當我運行程序時,我的向量是空的,儘管我的addItem()方法正確寫入文件。問題讀取文件與getline()

我試過四處尋找有關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

您是否打算在readFile函數中將myList向量作爲引用? – sajas 2014-11-24 04:51:55

+0

感謝sajas,你100%正確。我還擺脫了readFile()中的'else'語句,並將其更改爲 while(!read.eof()) { string item; getline(read,item); myList.push_back(item); } – TheNotoriousWMB 2014-11-24 05:08:51

回答

2

的READFILE的參數似乎是一個參考:

void readFile(vector<string>& myList) 

順便說一句,電流READFILE只是讀取單個線。

+0

是的,我注意到當我回去添加引用。出於某種原因,我對這種方法感到困惑,並且在不需要的時候使它成爲'if-else'。 – TheNotoriousWMB 2014-11-24 06:45:48