检索字符数组数据时遇到的问题 我对C++很陌生,我正在从事这个项目,我一直在努力绞尽脑汁。

检索字符数组数据时遇到的问题 我对C++很陌生,我正在从事这个项目,我一直在努力绞尽脑汁。,c++,arrays,database,fstream,seek,C++,Arrays,Database,Fstream,Seek,基本上,我希望在这个“数据库”中有10条用户记录。现在我的工资和年薪分别为double和int。我想最终把它们变成一个数组 我在这里面临几个问题: 当函数outputLine调用要从一名员工处查看的数据时,除了“name”部分之外,其他所有内容都显示得非常好,这部分会出现乱码。见下文: 显示名称的代码如下所示。我还包括我的insight结构: struct employee { int year_hired; double salary; char name[30]; int ID; void

基本上,我希望在这个“数据库”中有10条用户记录。现在我的工资和年薪分别为double和int。我想最终把它们变成一个数组

我在这里面临几个问题:

  • 当函数outputLine调用要从一名员工处查看的数据时,除了“name”部分之外,其他所有内容都显示得非常好,这部分会出现乱码。见下文:
  • 显示名称的代码如下所示。我还包括我的insight结构:

    struct employee {
    int year_hired;
    double salary;
    char name[30];
    int ID;
    void outputLine(ostream &output, employee &employed)
    {
    output << employed.getId() << endl
        << employed.getName() << endl
        << employed.getsal() << endl
        << employed.getYearHired() << endl;
    
    }
    void setname(string n) {
        const char *ptr = &name[30];
    
        ptr = n.c_str();
    
  • 当提示用户“添加”新帐户时,它会检查该帐户是否包含信息。即使我还没有设置该帐户,程序也会告诉我该帐户存储了信息。我的代码中有一部分专门用于构建10条空白记录以供输入,但它似乎并没有真正起到任何作用
  • 创建10条空白记录的节:

                ofstream outdatabase("database.dat", ios::out | 
    ios::binary);
                for (int i = 0; i < 10; i++) {
                    outdatabase.write(reinterpret_cast <const char *> 
    (&blankelist), sizeof(employee));
                }
    
    流输出数据库的
    (“database.dat”,ios::out |
    ios::二进制文件);
    对于(int i=0;i<10;i++){
    outdatabase.write(重新解释)
    (&blanklist),sizeof(雇员));
    }
    
    添加新记录的节:

    void newrecord(fstream &insertinfile) {
    
    int id = getid("Enter new employee ID");
    
    insertinfile.seekg((id - 1) * sizeof(employee));
    
    employee employed;
    insertinfile.read(reinterpret_cast <char *>(&employed), sizeof(employee));
    
    if (employed.getId() == 0)
    {
        string name;
        double salary;
        int year_hired;
    
        cout << "Enter name, balance, and year hired: " << endl;
        cin >> name;
        cin >> salary;
        cin >> year_hired;
    
        employed.setname(name);
        employed.setSalary(salary);
        employed.setYearHired(year_hired);
    
        employed.setId(id);
    
        insertinfile.seekp((id- 1) * sizeof(employee));
    
        insertinfile.write(reinterpret_cast<const char *> (&employed), 
    sizeof(employee));
    }
    else {
        cerr << "Employee ID: " << id
            << " already contains information." << endl;
    }
    
    }
    
    void newrecord(fstream和insertinfile){
    int id=getid(“输入新员工id”);
    插入文件。参见kg((id-1)*大小(员工));
    雇佣员工;
    insertinfile.read(重新解释演员阵容和雇用),sizeof(雇员));
    if(employed.getId()==0)
    {
    字符串名;
    双薪;
    年内聘请;
    姓名;
    cin>>工资;
    cin>>雇佣年份;
    使用。设置名称(名称);
    雇佣。固定工资(工资);
    雇佣。setYearHired(雇佣年);
    就业。setId(id);
    insertinfile.seekp((id-1)*sizeof(雇员));
    插入文件。写入(重新解释和使用),
    sizeof(雇员));
    }
    否则{
    
    cerr不能使用赋值将C字符串复制到指针,需要使用
    strcpy()


    是指向
    名称
    数组末尾之外的指针。数组的最后一个元素是
    名称[29]

    1“,它会发出乱码”当你可以很容易地复制粘贴时,不要发布屏幕截图。2)你是否尝试过使用调试器单步检查代码,以找出代码在哪里做了你意想不到的事情?3)“我已经提供了我目前为止所拥有的代码”你不应该提供完整的代码。你需要提供复制问题的代码。这太多代码了,无法查看。嘿,谢谢你的回复!我编辑了我的帖子以提供更多澄清嘿,谢谢你的回复!我在想如何使用strcpy()时遇到了麻烦。使用它有助于在程序调用函数时输出名称。实际上,当我使用strcpy()时,visual studio再次返回错误4996,并告诉我使用strcpy_s(),只是提醒一下。再次感谢您的回答!
    strcpy_s()
    是Microsoft特定的扩展。
                ofstream outdatabase("database.dat", ios::out | 
    ios::binary);
                for (int i = 0; i < 10; i++) {
                    outdatabase.write(reinterpret_cast <const char *> 
    (&blankelist), sizeof(employee));
                }
    
    void newrecord(fstream &insertinfile) {
    
    int id = getid("Enter new employee ID");
    
    insertinfile.seekg((id - 1) * sizeof(employee));
    
    employee employed;
    insertinfile.read(reinterpret_cast <char *>(&employed), sizeof(employee));
    
    if (employed.getId() == 0)
    {
        string name;
        double salary;
        int year_hired;
    
        cout << "Enter name, balance, and year hired: " << endl;
        cin >> name;
        cin >> salary;
        cin >> year_hired;
    
        employed.setname(name);
        employed.setSalary(salary);
        employed.setYearHired(year_hired);
    
        employed.setId(id);
    
        insertinfile.seekp((id- 1) * sizeof(employee));
    
        insertinfile.write(reinterpret_cast<const char *> (&employed), 
    sizeof(employee));
    }
    else {
        cerr << "Employee ID: " << id
            << " already contains information." << endl;
    }
    
    }
    
    if (n.size() >= sizeof name) {
            cout << "Name " << n << " is too long\n";
        }
        else {
            strcpy_s(name, n.c_str());
        }
    
    void updaterecord(fstream &updateFile) {
    
    int id = getid("Enter account to update");
    {
    
        updateFile.seekg((id - 1) * sizeof(employee));
        employee employed;
        updateFile.read(reinterpret_cast <char *> (&employed), sizeof(employee));
    
        if (employed.getId() != 0)
        {
            outputLine(cout, employed);
    
            cout << "\nEnter the new name, salary, and year hired: ";
            double salary;
            string name;
            int year_hired;
            cin >> name >> salary >> year_hired;
    
            double oldsalary = employed.getsal();
    
            employed.setname(name);
            employed.setSalary(salary);
            employed.setYearHired(year_hired);
    
            updateFile.seekp((id) * sizeof(employed));
    
        }
        else
        {
            cerr << "Account # " << id << " has no information." << endl;
        }
    }
    }
    
    updateFile.seekp((id - 1) * sizeof(employee));
    updateFile.write(reinterpret_cast <const char*> (&employed), 
    sizeof(employee));
    
    void setname(string n) {
        if (n.size() >= sizeof name) {
            cout << "Name " << n << " is too long\n";
        } else {
            strcpy(name, n.c_str());
        }
    }
    
    ptr = &name[30];