C ++ Как я могу получить все строки в двоичных файлах? - PullRequest
0 голосов
/ 18 ноября 2018

мой код ниже.

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". Я хочу получить всю строку "deneme1.txt" в while циклах, но я могу получить только последнюю строку (поэтому вывод «Создание файла ... 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 голосов
/ 19 ноября 2018

После некоторой очистки это не должно иметь неопределенного поведения.

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