2012-02-28 182 views
0

我發現的大部分信息都基於數字,但是我想使用單詞。舉例來說,如果我的文本文件看起來像這樣:從文本文件中讀取數據並將數據插入到數組中

M 
Gordon 
Freeman 
Engineer 
F 
Sally 
Reynolds 
Scientist 

我希望能夠把每行到一個數組和輸出它像這樣:

Gender: M 
First Name: Gordon 
Last Name: Freeman 
Job: Engineer 
Gender: F 
First Name: Sally 
Last Name: Reynolds 
Job: Scientist 

這個名單可以繼續下去,但現在兩個是好的。

我目前使用一個結構來保存信息:

struct PeopleInfo 
{ 
    char gender; 
    char name_first [ CHAR_ARRAY_SIZE ]; 
    char name_last [ CHAR_ARRAY_SIZE ]; 
    char job [ CHAR_ARRAY_SIZE ]; 
}; 

我不知道如果我需要使用分隔符或有事要告訴你的程序時停止在每個部分(性別,名字,姓氏等)。我可以在ifstream中使用getline函數嗎?我在自己的代碼中遇到了麻煩。我真的不知道從哪裏開始,因爲我現在不需要使用這樣的一段時間。瘋狂地搜索課本和谷歌找到類似的問題,但到目前爲止,我沒有太多的運氣。我會用我發現的任何問題和代碼更新我的文章。

回答

3

我覺得@ user1200129是正確的軌道上,但還沒有完全得到所有的作品還沒有放在一起。

我會改變結構只是一點點:

struct PeopleInfo 
{ 
    char gender; 
    std::string name_first; 
    std::string name_last; 
    std::string job; 
}; 

然後我會超載operator>>爲結構:

std::istream &operator>>(std::istream &is, PeopleInfo &p) { 
    is >> p.gender; 
    std::getline(is, p.name_first); 
    std::getline(is, p.name_last); 
    std::getline(is, p.job); 
    return is; 
} 

既然你希望能夠以顯示他們,我'd增加一個operator<<也這麼做:

std::ostream &operator<<(std::ostream &os, PeopleInfo const &p) { 
    return os << "Gender: " << p.gender << "\n" 
       << "First Name: " << p.name_first << "\n" 
       << "Last Name: " << p.name_last << "\n" 
       << "Job: " << p.job; 
} 

然後在af ILE充分的數據可以是這樣的:

std::ifstream input("my file name"); 

std::vector<PeopleInfo> people; 

std::vector<PeopleInfo> p((std::istream_iterator<PeopleInfo>(input)), 
          std::istream_iterator<PeopleInfo(), 
          std::back_inserter(people)); 

同樣,從矢量顯示人的信息去是這樣的:

std::copy(people.begin(), people.end(), 
      std::ostream_iterator<PeopleInfo>(std::cout, "\n")); 
+0

我想你在'>>中省略了幾個'p'。 – molbdnilo 2012-02-28 06:36:06

+0

@molbdnilo:喲,謝謝。固定。 – 2012-02-28 07:04:20

0

好讓你開始:

// Include proper headers here 
int main() 
{ 
    std::ifstream file("nameoftextfilehere.txt"); 
    std::string line; 
    std::vector<std::string> v; // Instead of plain array use a vector 

    while (std::getline(file, line)) 
    { 
     // Process each line here and add to vector 
    } 

    // Print out vector here 
} 
+0

嗯,沒事謝謝。我現在要玩這個。 – Hydlide 2012-02-28 01:29:29

1

一個結構可能比用於存儲信息的數組更好。

struct person 
{ 
    std::string gender; 
    std::string first_name; 
    std::string last_name; 
    std::string position; 
}; 

然後,你可以有一個人的矢量,並對其進行迭代。

+0

對不起,忘了添加我正在使用一個結構體。我添加了信息。我也會研究矢量。 – Hydlide 2012-02-28 01:38:47

0

您還可以使用像bool maleFlag和bool femaleFlag這樣的標誌,並將它們設置爲true和false,因爲當您只讀取一行上的'M'或'F'時,所以您知道與哪個性別關聯後面的名字。

0

你也可以用你的的std :: ifstream的文件任何其他流:

//your headers 
int main(int argc, char** argv) 
{ 
    std::ifstream file("name.txt"); 
    std::string line; 
    std::vector<std::string> v; // You may use array as well 

    while (file.eof() == false) { 
     file >> line; 
     v.push_back(line); 
    } 

    //Rest of your code 
    return 0; 
} 
+0

'while(file >> line)'會更好。 – devil 2012-02-28 01:58:38

相關問題