【问题标题】:To read from a text file and input into structure of array then displaying the data read C++从文本文件中读取并输入到数组结构中,然后显示读取的数据 C++
【发布时间】:2018-08-11 08:01:41
【问题描述】:

我在显示已读入结构数组的文件时遇到了一些问题。

这是我的结构:

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

我似乎无法将它输入到结构数组中并且无法显示出来?

【问题讨论】:

  • in.getline 对我来说似乎是错误的。请发minimal reproducible example
  • 你为什么不用std::string
  • 你是不是在网上放了一堆名字和他们的电子邮件地址?如果只是作为示例,请使用 ...@example.com,这是一个明确为此类内容保留的域。
  • @UlrichEckhardt 除非蜘蛛侠、超人和所有其他超级英雄都搬到了吉隆坡,否则我会认为这是假数据 ;)
  • 我的回答没有帮助吗?如果是这样,为什么不问问题/cmets。如果有帮助,为什么不接受和/或反对?

标签: c++ arrays output structure-of-arrays


【解决方案1】:

你的程序中有很长的错误列表!

1) 您提供的数据没有标签(可能只是复制粘贴到 SO 的问题) 所以请放心,您的下一篇文章将按照您的描述格式化!

2) 您的“电话号码”字段太短。数据记录中存在较长的电话号码

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

4) 我相信每一行的最后一个元素最后都不会包含一个制表符!所以该行中的最后一个 getline 应该读到 end 而不是 tab!

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 2017-06-25
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多