gpt4 book ai didi

c++ - 从文本文件读入数组结构

转载 作者:行者123 更新时间:2023-11-28 01:30:44 26 4
gpt4 key购买 nike

我正在尝试将文本文件读入结构数组。我还没有想出将它输入到结构数组中的方法,但我的代码的问题是输出不断循环。我是 C++ 的新手,因为这是我参加的第一门编程类(class)。

这是我放入的以“制表符”分隔的记录的文本文件。

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];
};

read的函数调用:

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;
}
}

关于c++ - 从文本文件读入数组结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51613497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com