首页 > 解决方案 > C++ 如何获取二进制文件中的所有行?

问题描述

我的代码在下面。

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”行,但我只能得到最后一行。(所以输出是“Creating File... 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”

标签: c++filebinary

解决方案


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

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

推荐阅读