C++ 如何读取二进制文件并搜索记录

C++ 如何读取二进制文件并搜索记录,c++,file-io,C++,File Io,我试图从文件中读取数据,并根据输入的员工编号搜索特定记录。我已经编写了代码,但是每次我搜索一条已经存在的记录时,我都会得到未找到的消息记录。谁能指出错误吗。 我的代码是: #include <iostream> #include <fstream> using namespace std; class emp { int empno; char name[20]; char dept[10]; float salary; public:

我试图从文件中读取数据,并根据输入的员工编号搜索特定记录。我已经编写了代码,但是每次我搜索一条已经存在的记录时,我都会得到未找到的消息记录。谁能指出错误吗。 我的代码是:

#include <iostream>
#include <fstream>

using namespace std;

class emp
{
    int empno;
    char name[20];
    char dept[10];
    float salary;
public:
    void getdata()
    {
        cout << "Enter the employee number " << endl;
        cin >> empno;

        cout << "Enter the name : " << endl;
        cin >> name;

        cout << "Enter the department of the employee : " << endl;
        cin >> dept;

        cout << "Enter the salary of the employee : " <<endl;
        cin >> salary;
    }

    void display()
    {
        cout << "Emp No : " <<empno;
        cout << endl << "Name : " << name << endl << "Department : " <<dept <<endl
             <<"Salary : " << salary <<endl;
    }

    int getempno()
    {
        return empno;
    }
};

int main()
{
    emp obj1;
    int eno;
    char ch = 'n';

    ifstream file1("emp.txt", ios:: in); // this file should already exist

    cout << "Enter the employee number to be searched for : " <<endl;
    cin >> eno;

    while(!file1.eof())
    {
        file1.read((char *)&obj1, sizeof(obj1));

        if(obj1.getempno()==eno)
        {
            obj1.display();
            ch = 'y';
            break;
        }
    }

    if(ch =='n')
        cout << "Record Not Found !!" << endl;
    file1.close();
}
#包括
#包括
使用名称空间std;
类电磁脉冲
{
int empno;
字符名[20];
char-dept[10];
浮动工资;
公众:
void getdata()
{
库特恩普诺;
姓名;
科特部;

cout以二进制形式打开流,如标题中所述:

   ifstream file1("emp.txt", ios:: in | ios::binary); // binary 
还可以更改循环,以便在未阅读以下内容的情况下不会对eof()进行测试:

while (file1.read((char *)&obj1, sizeof(obj1)))
我可以通过生成一个快速而脏的二进制文件,用ios::binary set编写,从而成功地测试更新后的代码(我不把构造函数代码放在这里):

void produceTest(字符串文件){
流操作系统(文件,ios::out | ios::binary);
环境管理计划a(1,“杜兰德”,“IT”,1234.30);
emp b(2,“杜邦”,“金融”,1530.20);
emp c(25,“克里斯”,“医学博士”,15.30);
os.write(重新解释铸造(&a),sizeof(emp));
操作系统写入(重新解释铸造(&b),尺寸(emp));
os.write(重新解释铸造(&c),sizeof(emp));
}
如果不起作用,则问题出在您的文件上。例如,潜在的问题可能是:

  • 该文件是在没有ios::binary的情况下编写的,导致结构发生更改(在windows上忽略0,将二进制字节0x0A转换为二进制0x0D+二进制0x0A)
  • 该文件是在一个使用不同整数编码的系统上编写的(big-endian与little-endian)
  • 该文件是用前导的unicode BOM表编写的
  • 文件的编码与您所想的不同

问题在于您读取文件的方式。为了清晰起见,请尝试打印文件内容。我认为问题可能出在“eno”中…它被存储为Char*,但与int相比…@user3095272在这里询问之前,您的任务是首先研究简单的解决方案。这样做的目的不是解释编译器错误消息,或者首先为您调试代码:P…您应该使用所有警告和调试信息进行编译(
g++-Wall-g
)然后使用调试器(例如,
gdb
),您将自己找到错误。您必须将记录存储为二进制blob吗?它不可移植。如果不是,我建议使用文本I/O函数>,写出各个字段。您还可以使用
std::string
s吗?
void produceTest(string file) {
    ofstream os(file, ios::out | ios::binary);
    emp a(1, "Durand", "IT", 1234.30); 
    emp b(2, "Dupond", "Finance", 1530.20); 
    emp c(25, "Chris", "MD", 15.30); 
    os.write(reinterpret_cast<char*>(&a), sizeof(emp)); 
    os.write(reinterpret_cast<char*>(&b), sizeof(emp));
    os.write(reinterpret_cast<char*>(&c), sizeof(emp));
}