簡體   English   中英

從文本文件讀入數組結構

[英]Reading from text file into a structure of array

我正在嘗試將文本文件讀入結構數組。 我還沒有找到一種將其輸入到結構數組中的方法,但是我的代碼的問題是輸出不斷循環。 我是C ++的新手,因為這是我參加的第一門編程課程。

這是我放入的記錄的文本文件,用“制表符”分隔。

1   Clark Kent  012-1449326 221, Jalan Pudu, Kuala Lumpur   clark_kent@gmail.com
2   Bruce Wayne 013-9817470 65, Jalan Jejaka, Kuala Lumpur  bruce_wayne@hotmail.com
3   Peter Parker 017-6912495    26, Jalan Rajabot, Kuala Lumpur peterparker@zoho.net
4   Yeoman Prince   014-1374040 22, Jalan 1/109e, Kuala Lumpur  yeoman_prince@yahoo.com
5   Tony Stark  016-7473151 21, Jalan Pandan, Kuala Lumpur  tonystark@zoho.net
6   Selina Kyle 012-4040928 Wisma Cosway, Kuala Lumpur  selina_kyle@gmail.com
7   Steve Rogers    018-9285217 Desa Pandan, Kuala Lumpur   steverogers@hotmail.com
8   Alan Scott  019-5569400 2, Jalan U1/17, Shah Alam   alanscott@gmail.com
9   Britt Reid  011-7876738 43, Jalan SS2/23, Petaling Jaya brittreid@yahoo.com
10  Darcy Walker    011-4042788 Blok B, Setapak, Kuala Lumpur   darcywalker@gmail.com
11  Reed Richards   019-2299339 Menara U, Bangsar, Kuala Lumpur reedrichards@zoho.net
12  Barbara Gordon  017-2297980 The Boulevard, Kuala Lumpur barbaragordon@gmail.com
13  Don Diego Vega  012-4142987 10, Jalan Wangsa, Kuala Lumpur  donvega@zoho.net
14  Billy Batson    013-9200151 122, Jalan Jejaka, Kuala Lumpur billybatson@hotmail.com
15  Barry Allen 017-7928822 Wisma Laxton, Kuala Lumpur  barryallen@gmail.com
16  Stanley Beamish 014-9177437 203, Sunwaymas, Batu Caves  stanleybeamish@yahoo.com
17  Dick Grayson    017-4023800 Pekeliling Bus, Kuala Lumpur    dickgrayson@hotmail.com
18  James Howlett   012-7816910 Sri Hartamas, Kuala Lumpur  jameshowlett@zoho.net
19  Hal Jordan  013-3439897 302, Jalan Ampang, Kuala Lumpur haljordan@yahoo.com
20  Scott Summers   012-9057100 Menara Summit, Subang Jaya  scottsummers@zoho.net

這是我的結構:

struct Employee {
int staffId;
char fullName[30];
char phoneNum[15];
char address[40];
char email[30];
};

函數調用讀取:

int main(void) {
int choice;
int value = 0;
Employee data;
menu();

cin >> choice;
do {
    if (choice == 1) {
        read();
    }
    else if (choice == 2) {
        add(value, &data);
    }
    else if (choice == 3) {
        list(value, &data);
    }
    else if (choice == 4) {
        search();
    }
    else if (choice == 5) {
        update();
    }
    else if (choice == 6) {
        deletes();
    }
    else if (choice == 7) {
        exit();
    }
    else {
        cout << "\n **Invalid choice option. Please enter from numbers 1 to 7 : ";
        cin >> choice;
    }

} while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7);

return 0;

}

這是我讀取文件的代碼:

void process(string* line) {
    cout << "line read: " << *line << endl;
}

void read()
{
    string line;
    ifstream in("list.txt");

    if (!in.is_open()) {
        cerr << "File can't be opened! " << endl;
    }
    while(getline(in,line)) {
        process(&line);
    }

    if (in.bad()) {
        cerr << "File can't be read! " << endl;
    }
    in.close();
    return;
}

這是我的輸出:

line read: 1    Clark Kent      012-1449326     221, Jalan Pudu, Kuala Lumpur   clark_kent@gmail.com
line read: 2    Bruce Wayne     013-9817470     65, Jalan Jejaka, Kuala Lumpur  bruce_wayne@hotmail.com
line read: 3    Peter Parker    017-6912495     26, Jalan Rajabot, Kuala Lumpur peterparker@zoho.net
line read: 4    Yeoman Prince   014-1374040     22, Jalan 1/109e, Kuala Lumpur  yeoman_prince@yahoo.com
line read: 5    Tony Stark      016-7473151     21, Jalan Pandan, Kuala Lumpur  tonystark@zoho.net
line read: 6    Selina Kyle     012-4040928     Wisma Cosway, Kuala Lumpur      selina_kyle@gmail.com
line read: 7    Steve Rogers    018-9285217     Desa Pandan, Kuala Lumpur       steverogers@hotmail.com
line read: 8    Alan Scott      019-5569400     2, Jalan U1/17, Shah Alam       alanscott@gmail.com
line read: 9    Britt Reid      011-7876738     43, Jalan SS2/23, Petaling Jaya brittreid@yahoo.com
line read: 10   Darcy Walker    011-4042788     Blok B, Setapak, Kuala Lumpur   darcywalker@gmail.com
line read: 11   Reed Richards   019-2299339     Menara U, Bangsar, Kuala Lumpur reedrichards@zoho.net
line read: 12   Barbara Gordon  017-2297980     The Boulevard, Kuala Lumpur     barbaragordon@gmail.com
line read: 13   Don Diego Vega  012-4142987     10, Jalan Wangsa, Kuala Lumpur  donvega@zoho.net
line read: 14   Billy Batson    013-9200151     122, Jalan Jejaka, Kuala Lumpur billybatson@hotmail.com
line read: 15   Barry Allen     017-7928822     Wisma Laxton, Kuala Lumpur      barryallen@gmail.com
line read: 16   Stanley Beamish 014-9177437     203, Sunwaymas, Batu Caves      stanleybeamish@yahoo.com
line read: 17   Dick Grayson    017-4023800     Pekeliling Bus, Kuala Lumpur    dickgrayson@hotmail.com
line read: 18   James Howlett   012-7816910     Sri Hartamas, Kuala Lumpur      jameshowlett@zoho.net
line read: 19   Hal Jordan      013-3439897     302, Jalan Ampang, Kuala Lumpur haljordan@yahoo.com
line read: 20   Scott Summers   012-9057100     Menara Summit, Subang Jaya      scottsummers@zoho.net
line read: 1    Clark Kent      012-1449326     221, Jalan Pudu, Kuala Lumpur   clark_kent@gmail.com
line read: 2    Bruce Wayne     013-9817470     65, Jalan Jejaka, Kuala Lumpur  bruce_wayne@hotmail.com
line read: 3    Peter Parker    017-6912495     26, Jalan Rajabot, Kuala Lumpur peterparker@zoho.net
line read: 4    Yeoman Prince   014-1374040     22, Jalan 1/109e, Kuala Lumpur  yeoman_prince@yahoo.com
line read: 5    Tony Stark      016-7473151     21, Jalan Pandan, Kuala Lumpur  tonystark@zoho.net

有什么方法可以建議它停止循環嗎? 我試圖避免將大小設置為20,因為在程序的另一部分中,我應該添加更多的員工記錄。 所以,我的問題是:

  1. 如何阻止它無限循環?
  2. 如何將讀取的行輸入到結構數組中?

先感謝您。

嘗試此操作,逐行讀取文件,然后根據定界符\\t解析每一行。

void readFile(const string& filename) {
    ifstream ifs(filename);

    string line;
    while (getline(ifs, line)) {
        istringstream iss(line);
        string token;

        Employee emp;

        while (getline(iss, token, '\t')) {
            // if you just want to print the information
            cout << token << '\t';
            // or you can store it in an Employee object
            // ...
        }
        cout << endl;
    }
}

問題是您永遠不會要求用戶提供新的菜單選項,因此您的程序將卡在第一選項中並無限循環。

cin >> choice;循環看起來像這樣cin >> choice; do循環內。

do {
    cin >> choice;
    if (choice == 1) {
        read();
    }
    ...

進行此更改后,您還需要重寫錯誤處理邏輯,但我將留給您。

正如Bob__在下面的注釋中所說,循環條件的邏輯是錯誤的choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 始終為 true,因此您的循環將永遠不會終止。

在任何情況下,您的整體程序邏輯中都有幾個錯誤,而這些都是您在進入單個菜單項的功能之前應該解決的錯誤。

問題的第二部分有點含糊。 您是要向結構中輸入數據,還是要創建一個類型為employee的數組,並將每個員工的數據放入該數組的索引中? 無論哪種情況,我都會回答后者。

關於第一個問題,停止無限循環很簡單。 while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7)是您的問題。 這行的邏輯是:“如果用戶輸入的數字不等於1或2或3 ...繼續執行循環。” 但是,這意味着無論用戶輸入什么數字,您的循環都將繼續。 假設您的目標是繼續要求用戶進行choice直到他們另行指定為止,那么更好的循環將是:

do{
    cin >> choice;
    ...
}
while(choice != 0);

在這種情況下,如果用戶輸入的數字不是0-7,程序將轉到cout << "\\n **Invalid choice option. Please enter from numbers 1 to 7 : "但是如果用戶輸入0 ,則程序結束。 您可以將0更改為任意值。

至於將數據輸入到結構中,過程非常簡單。 通常,您會使用重載>>運算符,但是現在這似乎超出了您當前的范圍,因此我們將以更基本的方式進行操作。 除非您確定要讀的員工數量,否則需要使用employee類型的向量。 為了方便使用,將您的char數組更改為結構中的字符串。 以下代碼是如何讀取數據的模板。

vector<employee> parse_text(){
    ifstream fin("lines.txt");
    vector<employee> employees;
    while(!fin.eof()){
        string temp_id, temp_fname, temp_lname;
        employee temp;
        fin >> temp_fname >> temp_lname;
        temp.fullName = temp_fname + temp_lname;
        fin >> temp_id;
        temp.staffId = (int)temp_id;
        ...
        employees.push_back(temp);
        }
    fin.close();
    return employees;
}

顯然,您將不得不添加幾行,但這足以使您入門。 當前,它會讀取員工的名字和姓氏,並將其連接起來,並將其存儲為員工的fullName 然后,它讀取員工的ID,將其強制轉換為int (因為getline存儲了一個字符串),然后將其存儲為staffId 沖洗並重復其余變量,您將擁有一個員工向量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM