Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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++_File_Binary - Fatal编程技术网

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

C++ C++;如何获取二进制文件中的所有行?,c++,file,binary,C++,File,Binary,我的代码在下面 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

我的代码在下面

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



}

我希望经过一些清理后,输出为“01 2 3”

,这不应该有任何未定义的行为

#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;
}
#包括
#包括
#包括
#包括
班级员工{
私人:
std::字符串名;
int-id;
浮动工资;
国际租赁公司;
公众:
员工(const std::string&name,int-id,float-salary,int-yearHired):姓名(name),id(id),薪水(salary),
年薪(年薪){}
Employee(Employee const&)=删除;
员工和操作员=(员工常量&)=删除;
int get_id()常量{
返回id;
}
};
int main(){
标准工资;
std::cout>year;
员工(姓名、本人、工资、聘用年限);
file.write((char*)和employee,sizeof(employee));
}
}//file.close();
std::ifstream文件(“deneme1.txt”,std::ios::in | std::ios::binary);
char*employeeData=新的char[sizeof(Employee)];
{
const Employee&Employee=*重新解释(employeeData);
而((file.read((char*)和employee,sizeof(employee))){

std::cout Binary files lines??如何在二进制文件中定义一行?请注意解释。显示您的类的标题。如果
name
是std::string,则无法执行此操作。我的二进制文件名为“deneme1.txt”,通常二进制文件没有
.txt
扩展名。@BatuhanÖzkan--这些代码都不起作用,所以请询问“为什么我不能用id做x”是无效的。如果你想证明,
sizeof(Employee)
等于多少?它将是相同的,不管
size()
这是
名称
提供给您的。那么如果
名称
为1000个字符,这应该如何工作?那么我如何解决这个问题?--您的文件布局如何?换句话说,其他应用程序如何读取您的文件并从文件中重新创建员工?这是您应该了解的第一步--它与编码无关。一旦你知道了这一点,那么它是否是二进制文件、文本文件等就无关紧要了。简而言之,这是对象序列化——谷歌搜索。但是输出是
0123
而不是
0123
。你不能
fwrite
包含
字符串的
struct
。你需要我需要序列化数据,而不是字符串对象。@BoR--您没有看到我关于为什么这永远无法工作的评论吗?
sizeof(Employee)
在ideone上运行时,不管名称中有多少个字符,都是48。所以这个解决方案不是解决方案。@PaulMcKenzie是的,看到了。但只要不接触std::string成员,这个“解决方案”就行了"。目前,它适用于POD数据类型。但是,如果您确实想要二进制格式,则需要定义它,而我没有花时间这样做。@RetiredInja正确,这就是我不接触该成员的原因,也是我必须将数据区域分配为字符数组以避免运行其构造函数和析构函数的另一个原因。
reinterpret_cast
应该是一个提示,说明这里有些棘手的事情。
#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;
}