c# 在Visual C中写入和阅读包含结构数据的二进制文件时出现意外行为

v2g6jxz6  于 2023-04-27  发布在  C#
关注(0)|答案(1)|浏览(173)

我挂在这个问题,因为早上没有找到解决方案.它也给出了各种错误,如,TESTING.exe已经触发了断点,以前它是抱怨堆损坏其他时间它是不能写数据等.我使用Visual Studio 2017 IDE,但只使用基本的C函数.如果有人能指出错误,这将是非常有帮助的我.下面是代码:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SAVE

typedef struct Employee
{
    char id[6];         
    char surname[20];
    char lastname[20]; 
    char firstname[20];
    unsigned int basic;
    unsigned int incentive;
    unsigned int salary;

} Employee;

typedef struct Database
{
    Employee* employee;
    unsigned int size;

} Database;

int SaveDatabase(const char* fileName, Database* db)
{
    FILE* fp = fopen(fileName, "wb");

    if (fp == NULL)
        return 1;

    if (fwrite(&db->size, sizeof(db->size), 1, fp) != 1)
    {
        fclose(fp);
        return 2;
    }

    if (fwrite(&db->employee, sizeof(db->employee), db->size, fp) != db->size)
    {
        fclose(fp);
        return 3;
    }

    fclose(fp);
    return 0;
}

Database* LoadDatabase(const char* fileName)
{
    Database* db = (Database*)malloc(sizeof(Database));

    unsigned int n = 0;

    FILE* fp = fopen(fileName, "rb");

    if (fread(&n, sizeof(n), 1, fp) != 1)
    {
        fclose(fp);
        return NULL;
    }

    db->employee = (Employee*)malloc(n * sizeof(db->employee));

    if (fread(&db->employee, sizeof(db->employee), n, fp) != n)
    {
        fclose(fp);
        return NULL;
    }

    fclose(fp);
    return db;
}

int main(int argc, char* argv[])
{

#ifdef SAVE

    Database* db = (Database*)malloc(sizeof(Database));
    db->size = 2;
    db->employee = (Employee*)malloc(db->size * sizeof(db->employee));

    strcpy(db->employee[0].id, "1234");
    strcpy(db->employee[0].surname, "Bakshi");
    strcpy(db->employee[0].lastname, "Vipin");
    strcpy(db->employee[0].firstname, "Byomkesh");
    db->employee[0].basic = 25000;
    db->employee[0].incentive = 25000;
    db->employee[0].salary = 50000;

    strcpy(db->employee[1].id, "1235");
    strcpy(db->employee[1].surname, "Chatterjee");
    strcpy(db->employee[1].lastname, "Naren");
    strcpy(db->employee[1].firstname, "Basu");
    db->employee[1].basic = 35000;
    db->employee[1].incentive = 30000;
    db->employee[1].salary = 65000;

    SaveDatabase("Data.dat", db);
    free(db->employee);
    free(db);

#else

    // loading database

    Database* db = LoadDatabase("Data.dat");

    for (unsigned int i = 0; i < db->size; i++)
    {
        printf("\n%s/t%s %s %s %u %u %u", db->employee[i].id, db->employee[i].firstname, db->employee[i].lastname, db->employee[i].surname
            , db->employee[i].basic, db->employee[i].incentive, db->employee[i].salary);
    }

    free(db->employee);
    free(db);

#endif // SAVE

    return 0;
}

在第一个示例中,定义了保存宏,它应该将数据保存到二进制文件中,当SAVE未定义时,它将从二进制文件中读取数据并显示数据。

nbnkbykc

nbnkbykc1#

db->employee是一个指针。将此指针写入文件不会保存数组的内容。
不要使用db->employee的地址,使用它作为指向要写入的数据的指针:

fwrite(db->employee, sizeof(*db->employee), db->size, fp)

反之,当阅读:

fread(db->employee, sizeof(*db->employee), n, fp)

相关问题