2013-06-01 21 views
1

我試着去閱讀這樣一個如何從一個文本文件中的一組值的閱讀,然後去下一行,做同樣的

James John 15 5 1 
Douglas Frank 23 8 1 
Bnejamin Zach 17 1 4 

列表以及每個值存儲到AA獨立的變量。名稱是字符串,其他數字是浮點數和int。到目前爲止,我可以從一行中獲取數據,但是我不知道如何進入下一行並執行相同操作。這是我的代碼到目前爲止。

#include <iostream> 
#include <string> 
#include <fstream> 


using namespace std; 

int main() 
{ 
ifstream employees; 
string lastname, firstname, lastname1, firstname1, lastname2, firstname2; 
float base, sales, base1, sales1, base2, sales2; 
int years, years1, years2; 


employees.open("employees.txt"); 

while (employees) 

    { 
employees >> lastname >> firstname >> base >> sales >> years; 

我想保持它儘可能簡單,我還不知道用戶定義的函數,數組或向量。那麼是否有一個簡單的函數會在多年後結束這一行;並去下一行繼續?

謝謝。

回答

0

您可以使用您的getline循環內檢索每個行,然後用用它stringstream

喜歡的東西:

string line; 
while(getline(employees,line)) 
{ 
    //doSomething 
} 

如果你不能使用數組他們輕鬆地存儲,就可以請把計數器知道你在哪一行,但這是非常重複的,並且文件中的行數不能變化:

string line; 
for (int count = 1 ; count <= 3 ; count++) 
{ 
    getline(employees,line); 
    istringstream iss(line); 
    if (count == 1) 
    { 
    iss >> lastname >> firstname >> base >> sales >> years; 
    } 
    else if (count == 2) 
    { 
    iss >> lastname1 >> firstname1 >> base1 >> sales1 >> years1; 
    } 
    else if (count == 3) 
    { 
    iss >> lastname2 >> firstname2 >> base2 >> sales2 >> years2; 
    } 
} 
2

使用陣列。當你結束了「我要一個號碼添加到這個變量,因爲我想不止一個」,如果數量達到2個以上,那麼你真的應該使用數組(除非非常特殊的情況下)。

您可能還需要使用結構來存儲您的不同的值(名字,姓氏,基地,銷售和年) - 這樣,你只能得到一個單一的陣列,而不是幾個不同的陣列。

由於這是C++,陣列裝置vector。換句話說:

struct employee 
{ 
    string firstname, lastname; 
    float base, sales; 
    int years; 
}; 


vector<employee> emp_table; 

employee e; 

while (employees >> e.firstname >> e.lastname >> e.base >> e.sales >> e.years) 
{ 
    emp_table.push_back(e); 
} 

注意我把僱員的輸入作爲while-condition。這可以避免額外的循環迭代,並在到達文件結尾時「推回」最後一個條目的第二個副本。

1

有在C++中許多方法來完成你正在嘗試做的。一種方法,它允許數據驗證是使用std::getline功能在一次讀取文件的一行,然後使用std::stringstream分析數據。這可以讓您對數據進行驗證,並繼續處理如果某行的數據是畸形。

[作爲墊指出可以使用的數據結構和std::vector進行存儲和管理數據更加容易。]

#include <fstream> 
#include <sstream> 
#include <string> 
#include <vector> 


struct employee 
{ 
    std::string firstname; 
    std::string lastname; 
    float base; 
    float sales; 
    int years; 
}; 

int main() 
{ 

    std::ifstream employeeFile; 
    employeeFile.open("employees.txt"); 

    std::string tmpLine; 
    std::vector<employee> employeeTable; 

    // read in an entire line at a time 
    while(std::getline(employeeFile, tmpLine)) 
    { 
     // Place the input line into a stream that reads from 
     // a string instead of a file. 
     std::stringstream inputLine(tmpLine); 


     // Try parsing the data. The ! operator is used here to check 
     // for errors. Since we expect the data to be in a specific format 
     // we want to be able to handle situations where the input line 
     // may be malformed. For example, encountering a string where 
     // a number should be. 
     employee e; 
     if(!(inputLine >> e.firstname >> e.lastname >> e.base >> e.sales >> e.years)) 
     { 
      // ... error parsing input. Report the error 
      // or handle it in some other way. 

      continue; // keep going! 
     } 

     // Add to the vector 
     employeeTable.push_back(e); 
    } 

    return 0; 
} 
0

打開和讀取文件的正確比學習數組是什麼困難。如果您不使用數組,則必須使用太多變量來保存所有數據,並且必須重複編寫代碼以從文件讀取數據,而不是一次寫入循環。

#include <iostream> 
#include <string> 
#include <fstream> 

using namespace std; 

int main() 
{ 

    string firstNames[3]; 
    string lastNames[3]; 
    float heights[3]; 
    float weights[3]; 
    int ages[3]; 

    ifstream infile("data.txt"); 

    if(!infile) 
    { 
     cout << "Couldn't open file!" << endl; 
     return 1; 
    } 

    int count = 0; 

    while (infile >> firstNames[count] 
        >> lastNames[count] 
        >> heights[count] 
        >> weights[count] 
        >> ages[count]) 
    { 

     ++count; 
    } 


    infile.close(); 

    for (int i = 0; i<count; ++i) { 
     cout << firstNames[i] << " " 
      << lastNames[i] << " " 
      << heights[i] << " " 
      << weights[i] << " " 
      << ages[i] << " " << endl; 
    } 


    return 0; 
} 


--output:-- 
James John 15 5 1 
Douglas Frank 23 8 1 
Bnejamin Zach 17 1 4 

相較於這場災難:

#include <iostream> 
#include <string> 
#include <fstream> 

using namespace std; 

int main() 
{ 

    string firstName0,firstName1, firstName2; 
    string lastName0, lastName1, lastName2; 
    float height0, height1, height2; 
    float weight0, weight1, weight2; 
    int age0, age1, age2; 

    ifstream infile("data.txt"); 

    if(!infile) 
    { 
     cout << "Couldn't open file!" << endl; 
     return 1; 
    } 

    infile >> firstName0 >> lastName0 >> height0 >> weight0 >> age0; 
    infile >> firstName1 >> lastName1 >> height1 >> weight1 >> age1; 
    infile >> firstName2 >> lastName2 >> height2 >> weight2 >> age2; 


    infile.close(); 

    cout << firstName0 << " " 
     << lastName0 << " " 
     << height0 << " " 
     << weight0 << " " 
     << age0 << endl; 

    cout << firstName1 << " " 
     << lastName1 << " " 
     << height1 << " " 
     << weight1 << " " 
     << age1 << endl; 

    cout << firstName2 << " " 
     << lastName2 << " " 
     << height2 << " " 
     << weight2 << " " 
     << age2 << endl; 

    return 0; 
} 

--output:-- 
James John 15 5 1 
Douglas Frank 23 8 1 
Bnejamin Zach 17 1 4 

看看你有重複的代碼。請注意,當您使用數組時,變量名稱將變爲firstNames [0](v.firstName0),lastNames [0](v.namename0)等,firstNames [1](v.firstName1)和lastNames [1](v。lastName0)。

+0

您需要檢查'count'的值,否則您最終會遇到_Indiana Jones和未定義Behavior_王國。 –

相關問題