【问题标题】:Read/Write from fstream - help needed从 fstream 读取/写入 - 需要帮助
【发布时间】:2014-04-10 02:26:17
【问题描述】:

在为 C++ 类工作的项目中遇到了困难。我们正在使用固定长度的员工记录练习文件 i/o。我正在很好地创建员工,将它们存储到 unique_ptr 的向量中,然后尝试循环遍历这些,写入文件,清除向量,然后将它们从文件中读回向量中。

问题似乎与我尝试将它们读回的方式有关。写入后,文件中的所有内容看起来都是正确的,我可以说。以下是我正在尝试做的事情。 但是,在读取 Employee 记录后,它会额外循环一次并且无法识别文件的结尾,从而用垃圾数据填充成员。我不知道为什么,因为我是写出准确的字节数,然后读回相同的数字。

当从文件中将员工记录读回向量中时,做错了吗:

while (emprecs.good())

我怀疑这是让我遇到麻烦的原因,但不知道如何测试/调试它。这是适用的代码:

Employee.h

class Employee{
    int id;
    std::string name;
    std::string address;
    std::string city;
    std::string state;
    std::string country;
    std::string phone;
    double salary;

    struct EmployeeRec{ // Employee file for transfers
        int id; 
        char name[31];
        char address[26];
        char city[21];
        char state[21];
        char country[21];
        char phone[21];
        double salary;
};

    void write(std::ostream&) const;
    static std::unique_ptr<Employee> read(std::istream&);
};

Employee.cpp

// Write a fixed-length record to current file position
void Employee::write(ostream& os) const{
    EmployeeRec outbuf;
    outbuf.id = id;
    strncpy(outbuf.name, name.c_str(), 30)[30] = '\0';
    strncpy(outbuf.address, address.c_str(), 25)[25] = '\0';
    strncpy(outbuf.city, city.c_str(), 20)[20] = '\0';
    strncpy(outbuf.state, state.c_str(), 20)[20] = '\0';
    strncpy(outbuf.country, country.c_str(), 20)[20] = '\0';
    strncpy(outbuf.phone, phone.c_str(), 20)[20] = '\0';
    outbuf.salary = salary;
    os.write(reinterpret_cast<const char*>(&outbuf), sizeof outbuf);
}

// Read record from current file position
std::unique_ptr<Employee> Employee::read(std::istream& is){
    EmployeeRec inbuf;
    is.read(reinterpret_cast<char*>(&inbuf), sizeof inbuf);
    unique_ptr<Employee> emp(new Employee());
    if (is.good()) {
        emp->id = inbuf.id;
        emp->name = inbuf.name;
        emp->address = inbuf.address;
        emp->city = inbuf.city;
        emp->state = inbuf.state;
        emp->country = inbuf.country;
        emp->phone = inbuf.phone;
        emp->salary = inbuf.salary;
    }
    return emp;
}

Driver.cpp

// employee.bin is a new file that I will be writing to and then reading from
// Write all employee's currently in the vector to the file
fstream emprecs("employee.bin", ios::in | ios::out | ios::binary | ios::trunc); 
    _ASSERT(emprecs.is_open()); 
    for (size_t i = 0; i < EmpVect.size(); ++i){
                // EmpVect is a vector of unique_ptr<Employee>
        (EmpVect[i])->write(emprecs);
    }

// Clear the vector in preparation to read the employees back in from the file
EmpVect.clear();

// Move back to beginning of stream
emprecs.seekp(0, ios::beg);

// Read employees back in from the file
// This loops an extra time with junk data... can't figure out why, though
// Shouldn't the failbit be set once I reach end of file? I am missing something here.
while (emprecs.good()){
    EmpVect.push_back(std::move(Employee::read(emprecs)));
}

【问题讨论】:

    标签: c++ stream fstream istream


    【解决方案1】:

    开启std::unique_ptr&lt;Employee&gt; Employee::read(std::istream&amp; is)

    is.good() return false 时,emp 仍然返回,它是一个结构体,不会用 constucter 初始化(所以用垃圾数据)。

    【讨论】:

    • Aaaahhh,这很有意义。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2011-09-07
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多