2014-10-05 18 views
-2

我試圖從一個.txt文件讀取數據,並將其內容放入鏈接列表中,每個節點包含兩個字符串。由於.txt文件中的某些字符串包含空格,因此我寧願將它們保留爲空格而不是空格,因此我使用std :: getline()。std :: getline「混合」在一起的字符串

我格式化爲這樣的.txt文件:

「旅遊,娛樂&體育管理」

「TRS」

「人類學」

「蟻族」

等等,但沒有每行之間的空白行。鏈表有一個print()方法,用這種格式打印數據:data1/data2;數據1 /數據2:

所以,當我打印一個節點與數據1 ==「旅遊,娛樂&體育管理」和數據2 ==「TRS」所需的輸出是:

「旅遊,娛樂&體育管理「/」TRS「;

不過,我實際得到的是:

「TRS」主義,娛樂&體育管理」

然而,當我讀到行,並將其分配給字符串,然後打印出這些字符串無將它們插入到鏈表中,我得到正確的輸出,

std::cout << data1 << std::endl; 
std::cout << data2 << std::endl; 

將正確的輸出:

「旅遊,休閒&運動管理」
「TRS」

什麼給?

編輯: 鏈表頭:

#ifndef _2DLL_H_ 
#define _2DLL_H_ 
#include <iostream> 
#include <string> 



class Llist{ 
    struct node{ 
    //node member var 
    std::string department; 
    std::string abv; 
    node * next; 

    //node member methods 

    std::string search(std::string dep); 
    void print(); 
    void remove(const std::string dep); 
    void clear(); 
    //constructer 
    node(const std::string dep , const std::string a):department(dep), abv(a), next(NULL){} 

    };// end node 

public: 
    //Llist member var 
    node * head; 

    //LList constructer & destructor 
Llist():head(NULL){} 
    ~Llist(){clear();} 

    //Llist member functions 
    std::string search(const std::string dep); 
    void insert(const std::string dep , const std::string a); 
    void print(); 
    void remove(const std::string dep); 
    void clear(); 
    const int operator[](unsigned int index)const; 
};// end Llist 


#endif //_2DLL_H_ 

鏈表的.cpp

#include "2DLL.h" 
#include <algorithm> 


//=========INSERT============== 

void Llist::insert(const std::string dep, const std::string a){ //will just prepend. Fuck the police; 
    node * n = new node(dep , a); 
    n->next = head; 
    head = n; 
} 

//========PRINT================= 
void Llist::print(){ 
    if(head==NULL){ 
    std::cout << "ERROR: List is empty" << std::endl; 
    } 
    else{ 
    head->print(); 
    } 
} 

void Llist::node::print(){ 
    if(next==NULL){ 
    std::cout << department << ";" << abv << std::endl; 
    } 
    else{ 
    std::cout << department << ";" << abv << "/" ; 
    std::cout << std::endl; 
    next->print(); 
    } 
} 

//=======REMOVE======== 

void Llist::remove(const std::string dep){ 
    if(head==NULL){ 
    std::cout << "ERROR: List is empty" << std::endl; 
    } 
    else{ 
    head->remove(dep); 
    } 
} 

void Llist::node::remove(const std::string dep){ 
    if(next->department == dep){ 
    node * n = next; 
    next = n->next; 
    delete n; 
    } 
    else{ 
    next->remove(dep); 
    } 
} 

//===========CLEAR()================== 

void Llist::clear(){ 
    if(head==NULL){ 
    std::cout << "ERROR:List is empty" << std::endl; 
    } 
    else{ 
    head->clear(); 
    head = NULL; 
    } 
} 

void Llist::node::clear(){ 
    if(this==NULL){ 
    return;  

    } 
    else{ 
    next->clear(); 
    delete this; 
    } 

} 

//=========OPERATOR============= 
/* 
const int Llist:: operator[] (unsigned int index) const{ 
    node * n = head; 
    for(int i = 0 ; i < index && n!=NULL ; ++i){ 
    n=n->next; 
    } 
    return n->data; 
} 

*/ 

//========SEARCH==================== 

std::string Llist::search(std::string dep){ 
    if(head == NULL){ 
    std::cout << "ERROR: List is empty" << std::endl; 
    return "ERROR"; 
    } 
    else{ 
    //dep.erase(std::remove(dep.begin(),dep.end(),' '),dep.end()); 
    //std::cout << dep << std::endl; 
    return head->search(dep); 
    } 
} 

std::string Llist::node::search(std::string dep){ 
    if(department == dep){ 
     return abv; 
    } 
    else{ 
     return next->search(dep); 
    } 
    } 

的讀

#include "genCollege.cpp" 
#include "genDepartment.cpp" 
#include "2DLL.cpp" 
#include <ctime> 
#include <fstream> 

using namespace std; 


int main(){ 
    std:: ifstream file; 
    file.open("DepList.txt"); 
    std::string department; 
    std::string abv; 

    srand(time(0)); 
    /*for (int i = 0 ; i < 30 ; i++){ 
     std::string college = genCollege(); 
     std::string department = genDepartment(college); 

     std::cout << "College: "<< college << std::endl; 
     std::cout << "\t" << "Department: " << department << std::endl; 
     std::cout << std::endl; 
    } */ 
    Llist list; 

    while(file.is_open()){ 
     if(file.eof()){break;}; 

     std::getline(file , department); 
     std::getline(file, abv); 

     list.insert(department , abv); 

    } 
    //file.close(); 
    list.print(); 



    return 0 ; 
} 
+1

5頁的散文(格式不好),沒有代碼?是什麼賦予了? – 2014-10-05 21:49:22

+0

聽起來像你的節點沒有正確地爲每個字符串分配獨立的存儲空間。節點是否包含'std :: string'? '炭[]'? char *'? – 2014-10-05 21:50:40

+0

我想通常使用getline()方法可能只是一個怪癖,而我並不知道。你想看什麼代碼?您只需要詢問:) – 2014-10-05 21:51:44

回答

0

作爲納米建議的用戶,它似乎是因爲實施我正在閱讀Windows的文本文件,並在Ubuntu上運行程序,outp ut看起來不對。他的單詞回答是:

「您有一個爲Windows創建的文本文件,其中包含\r\n作爲行終止符。您的程序要麼在un * x上運行,要麼無法在文本模式下打開文件。 \r在每個字符串,它弄亂你的終端窗口的結束。「

他建議我檢查,看看是否在字符串中的最後一個字符我用std::getline()\r,如果是,將其刪除來自字符串。我簡單地做字符串的一個子問題後,我與std::getline()

我然後插入新的子到鏈表並根據需要將print()方法現在輸出收購他們這樣做。