C ++如何获取二进制文件中的所有行?

时间:2018-11-18 15:04:10

标签: c++ file binary access

我的代码在下面。

int main() {
    Employee* employee = new Employee();
    cout << "Creating File..." << endl;
    fstream file("deneme1.txt",ios::out|ios::binary|ios::ate);
    for(int i=0;i<10;i++){
        if(i<4){
            string name;
            float salary;
            int yearHired;
            cout<<"Enter Name: ";
            cin>>name;
            cout<<"Enter Salary: ";
            cin>>salary;
            cout<<"Enter Year Hired: ";
            cin>>yearHired;
            employee->set_id(i);
            employee->set_name(name);
            employee->set_salary(salary);
            employee->set_yearHired(yearHired);
            file.write((char*)&employee,sizeof(employee));
            file.close();
             }

         }
        file.open("deneme1.txt",ios::in|ios::out|ios::binary);

        file.seekg(0);


        while((file.read((char*)&employee,sizeof(employee)))) {
   cout << employee->get_id();
}



}

我有一个名为“ deneme1.txt”的二进制文件。我将四个对象记录到“ deneme1.txt”。我想在while循环中获得“ deneme1.txt”的所有行,但是我只能得到最后一行。(因此输出为 “正在创建文件... 3“ )

如何获取文件中的所有行?也许文件只有一个对象?

我在下面的课

class Employee
{
private:
    string name; 
    int id;
    float salary;
    int yearHired;

public:
    void set_name(string n)
    {
        this->name = n;
    }
    void set_id(int i)
    {
        this->id = i;
    }
    string get_name()
    {
        return name;
    }
    int get_id()
    {
        return id;
    }


    void set_salary(float s)
    {
        this->salary = s;
    }

    float get_salary()
    {
        return salary;
    }

    void set_yearHired(int y)
    {
        this->yearHired = y;
    }

    int get_yearHired()
    {
        return yearHired;
    }


};

我希望输出为“ 0 1 2 3”

1 个答案:

答案 0 :(得分:-1)

经过一些清理,这应该没有任何未定义的行为。

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

class Employee {
private:
    std::string name;
    int id;
    float salary;
    int yearHired;
public:
    Employee(const std::string &name, int id, float salary, int yearHired) : name(name), id(id), salary(salary),
                                                                             yearHired(yearHired) {}
    Employee(Employee const&) = delete;
    Employee& operator=(Employee const&) = delete;
    int get_id() const {
        return id;
    }
};

int main() {
    std::cout << "Creating File..." << std::endl;
    {
        std::ofstream file("deneme1.txt", std::ios::out | std::ios::binary | std::ios::ate);
        std::string name;
        for (int i = 0; i < 4; i++) {
                float salary;
                int yearHired;
                std::cout << "Enter Name: ";
                std::cin >> name;
                std::cout << "Enter Salary: ";
                std::cin >> salary;
                std::cout << "Enter Year Hired: ";
                std::cin >> yearHired;
                Employee employee(name, i, salary, yearHired);
                file.write((char *) &employee, sizeof(employee));
        }
    }  // file.close();

    std::ifstream file("deneme1.txt", std::ios::in | std::ios::binary);

    char* employeeData = new char[sizeof(Employee)];
    {
        const Employee &employee = *reinterpret_cast<const Employee *>(employeeData);
        while ((file.read((char *) &employee, sizeof(employee)))) {
            std::cout << employee.get_id();
        }
    }
    delete[]employeeData;

    return EXIT_SUCCESS;
}