在将读取的文件显示到结构数组中时遇到一些麻烦。
这是我的结构:
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;
}
我似乎无法以结构数组输入它,也无法显示出来?
答案 0 :(得分:1)
您的编中有很多错误!
1)您提供的数据没有标签(也许只是复制粘贴到SO的问题) 因此,请确保下次发布时保持您的数据格式如所述!
2)您的“电话号码”字段较短。数据记录中存在更长的电话号码
3)对于字段的大小,应使用常量而不是硬编码值。如果在一个地方更改字段大小,则有可能在其他地方错过该字段!看看例子!
4)每行的最后一个元素在结尾处都不会包含制表符!因此,该行中的最后一个getline应该读到直到tab结束为止!
5)打印数据的循环必须使用循环var i
而不是循环value
,它是第一个元素BEHIND的编号!您的数据!
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;
}
}