Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++;_C++_Arrays_Output_Structure Of Arrays - Fatal编程技术网

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

C++ 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++;,c++,arrays,output,structure-of-arrays,C++,Arrays,Output,Structure Of Arrays,我在显示已读入结构数组的文件时遇到一些问题 这是我的结构: struct Employee { char staffId[5]; char fullName[30]; char phoneNum[15]; char address[40]; char email[30]; }; 这是我的文本文件(以“tab”分隔的列): 要从文本文件中读取的我的代码: ifstream in("list.txtwith extension"); Employee *t

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

这是我的结构:

struct Employee {
    char staffId[5];
    char fullName[30];
    char phoneNum[15];
    char address[40];
    char email[30];
 };
这是我的文本文件(以“tab”分隔的列):

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

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;
    }
ifstream-in(“list.txt带扩展名”);
员工*totaldata=新员工[值+1];
弦线;
while(getline(in,line)){
istringstream iss(线);
字符串标记;
while(getline(iss,token,'\t')){
//如果你只是想打印信息

你的程序中有一长串bug

1) 您提供的数据没有选项卡(可能只是复制粘贴到SO时出现问题) 因此,请在下一篇文章中使用舒尔,以便您的数据按所述格式格式化

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

3) 对于字段大小,应该使用常量而不是硬编码的值。如果在一个位置更改字段大小,则在其他位置可能会忽略该字段大小!参见示例

4) 我相信,每行的最后一个元素的末尾不会包含制表符!因此,行中的最后一个getline应该读取到end而不是tab

5) 用于打印数据的循环必须使用循环var
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;

    }
}
struct Employee{
静态常量unsigned int staffIdLen=5;
静态常量unsigned int fullNameLen=30;
静态常量unsigned int phoneNumLen=20;
静态常量无符号整数地址len=40;
静态常量unsigned int emailLen=30;
煤渣;
字符全名[全名];
char phoneNum[phoneNumLen];
字符地址[addressLen];
char email[emailLen];
};
int main()
{
常量无符号整数maxRecords=10;
std::ifstream in(“f.txt”);
员工*totaldata=新员工[maxRecords];
std::字符串行;
无符号整数值=0;
while(std::getline(in,line))
{

std::cout
in.getline
对我来说似乎是错误的。请做一个简单的解释。你为什么不使用
std::string
你刚才把一堆名字和他们的电子邮件地址放在网上了吗?如果只是作为一个例子,请使用
。@example.com
,这是一个明确为类似的东西保留的域。@UlrichEckhardt其他超级英雄搬到了吉隆坡,我认为这是伪造的数据;)我的回答没有帮助吗?如果有,为什么不提问/评论。如果有帮助,为什么不接受或投票?
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;

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

}