从文本文件中读取并输入到数组结构中,然后显示读取的数据C++



我在显示我读到的结构数组中的文件时遇到了一些麻烦。

这是我的结构:

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

这是我的文本文件(列由"制表符"分隔(:

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

我要从文本文件中读取的代码:

ifstream in("list.txtwith extension");
Employee *totaldata = new Employee[value + 1];
string line;
while (getline(in, line)) {
istringstream iss(line);
string token;
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
in.getline(totaldata[value].staffId, 5, 't');
in.getline(totaldata[value].fullName, 30, 't');
in.getline(totaldata[value].phoneNum, 15, 't');
in.getline(totaldata[value].address, 40,'t');
in.getline(totaldata[value].email, 30, 't');
value++;
}
cout << endl;
for (int i = 0; i < value; i++) 
{
cout << totaldata[value].staffId << "t"
<< totaldata[value].fullName << "t"
<< totaldata[value].phoneNum << "t"
<< totaldata[value].address << "t"
<< totaldata[value].email << endl;
}

我似乎无法在结构数组中输入它并且无法显示它?

你的程序中有一长串错误!

1(您提供的数据没有选项卡(可能只是复制和粘贴到SO的问题( 因此,请为您的下一篇文章提供舒尔,因此您的数据已按照描述进行格式化!

2(您的字段"电话号码"是短的。数据记录中存在较长的电话号码

3( 对于字段的大小,您应该使用常量而不是硬编码值。如果您在一个地方更改字段大小,则有可能在其他地方错过该字段大小!看例子!

4(我相信每行的最后一行的最后一个元素不会在末尾包含制表符!因此,行中的最后一个获取行应该一直读到结束,而不是直到选项卡!

5(打印数据的循环必须使用循环变量i而不是value这是后面第一个元素的编号!您的数据!

6(你的阅读循环完全混乱了。我把它缩小了一点;)

7( 您使用固定大小的元素/记录。但是你不检查超限!因此,如果值变为 11,您的程序就会崩溃!

8(作为一般评论:你的程序是C代码!您不使用对象,而是直接写入该数据结构的纯数据结构和函数。这不是面向对象的,并且存在很多问题,例如覆盖字段大小,不检查范围以及数据不一致!

我在最后添加了一个更 c++ 风格的示例。这个例子也不是很完美,因为它在处理过程中需要大量的字符串副本,这使得它比旧的 c 样式代码慢。但它应该有助于让你有一个使用对象的想法,这些对象知道如何自己做动作,比如读/写自己的数据。如前所述:仅作为起点,离真正好还很远!

struct Employee {
static const unsigned int staffIdLen = 5;
static const unsigned int fullNameLen = 30;
static const unsigned int phoneNumLen = 20;
static const unsigned int addressLen = 40;
static const unsigned int emailLen = 30;
char staffId[staffIdLen];
char fullName[fullNameLen];
char phoneNum[phoneNumLen];
char address[addressLen];
char email[emailLen];
};

int main()
{
const unsigned int maxRecords = 10;
std::ifstream in("f.txt");
Employee *totaldata = new Employee[ maxRecords ];
std::string line;
unsigned int value = 0;
while ( std::getline(in, line) )
{
std::cout << "Read from file:" << line << std::endl;
std::istringstream iss(line);
iss.getline(totaldata[value].staffId, Employee::staffIdLen, 't');
iss.getline(totaldata[value].fullName, Employee::fullNameLen, 't');
iss.getline(totaldata[value].phoneNum, Employee::phoneNumLen, 't');
iss.getline(totaldata[value].address, Employee::addressLen,'t');
iss.getline(totaldata[value].email, Employee::emailLen);
value++;
if ( value == maxRecords ) break;
}
std::cout << "Finish reading file " << std::endl;
for (int i = 0; i < value; i++)
{
std::cout << "----------------start-----------" << std::endl;
std::cout << totaldata[i].staffId << "t"
<< totaldata[i].fullName << "t"
<< totaldata[i].phoneNum << "t"
<< totaldata[i].address << "t"
<< totaldata[i].email << std::endl;
std::cout << "--------------end--------------" << std::endl;
}
}

C++ 示例:

class Employee {
private:
std::string staffId;
std::string fullName;
std::string phoneNum;
std::string address;
std::string email;
public:
friend std::istream& operator>>( std::istream&, Employee& );
friend std::ostream& operator<<( std::ostream&, const Employee& );
};
std::istream& operator>>( std::istream& is, Employee& e)
{
std::getline( is, e.staffId, 't');
std::getline( is, e.fullName, 't');
std::getline( is, e.phoneNum, 't');
std::getline( is, e.address, 't');
std::getline( is, e.email );
return is;
}
std::ostream& operator<<( std::ostream& os, const Employee& e)
{
os << e.staffId << e.fullName << e.phoneNum << e.address << e.email << std::endl;
return os;
}
int main()
{
std::ifstream in("f.txt");
std::vector<Employee> employees;
do
{
Employee e;
in >> e;
if ( in.fail() ) break;
employees.push_back( e );
} while( 1 );
for ( auto& e: employees)
{
std::cout << e << std::endl;
}
}

最新更新