简体   繁体   English

将数据从类数组写入二进制文件C ++

[英]Writing Data from an Array of Classes to a Binary File C++

In my code I have an Array of class User as a private variable in class UserManager. 在我的代码中,我将User类的数组作为UserManager类中的私有变量。 When UserManager::Shutdown() is called it save the Array of Users to a file. 调用UserManager :: Shutdown()时,它将用户数组保存到文件中。 Yet I get an access violation error. 但是我收到访问冲突错误。 The Access violation error occurs in fwrite.c fwrite.c中发生访问冲突错误

I am compiling on Visual Studio 2010 running Windows 7. 我正在运行Windows 7的Visual Studio 2010上进行编译。

User Class 用户类别

class User {
public:
User() {
    memset(username, 0, 255);
    memset(password, 0, 255);
}

int userId;
char username[255];
char password[255];
};

MAX_USERS Definition MAX_USERS定义

#define MAX_USERS 2048

User Manager Constructor 用户管理器构造函数

UserManager::UserManager() {
memset( users, 0, MAX_USERS);
}

Shutdown Function 关机功能

void UserManager::Shutdown( void) {
FILE *userDB = fopen( "users.bin", "wb");
int k=1;
for(k=1;k<MAX_USERS-1;k++){
    if (users[k]!=NULL) {
        fwrite((const void*)&users[k]->userId, sizeof(int), 1, userDB);
        fwrite((const void*)users[k]->username, 255, 1, userDB);
        fwrite((const void*)users[k]->password, 255, 1, userDB);
    } else {
        fpos_t skip = 255 + 255 + sizeof(int);
        fsetpos( userDB, &skip);
    }
}

fclose( userDB);
}

The array 'users' is memset to zero at the constructor. 数组“用户”在构造函数中被设置为零。

Theory 1 理论1

Your loop iteration starts at 1, but array indexing starts at 0. Without seeing the definition of MAX_USERS or the actual error my guess is your k value is overrunning your users array boundary. 您的循环迭代从1开始,但数组索引从0开始。在没有看到MAX_USERS的定义或实际错误的情况下,我的猜测是您的k值超出了用户数组边界。

Theory 2 理论2

You declared users as 您将用户声明为

User *users[MAX_USERS] = {};

Then you didn't new up each user when you created them. 这样,您在创建每个用户时就不会新建它们。 So, instead of 所以,代替

users[index] = new User();

you did 你做了

  User user1;
  users[index] = &user1;

The second version would probably go out of scope. 第二个版本可能超出范围。 Then the memory for user1 would be reallocated somewhere else and when you tried to access it via users[k]-> you get the access violation. 然后,将user1的内存重新分配到其他地方,并且当您尝试通过users[k]->访问它时,会遇到访问冲突。

My version 我的版本

Below is the code I used to try figure out the problem. 下面是我用来尝试找出问题的代码。 It's quick and dirty with me filling in the gaps but it runs to completion. 我填补了空白,既快速又肮脏,但是可以完成。 Hopefully it will help. 希望它会有所帮助。

#define MAX_USERS 2048
class User { 
    public: User() {     
    memset(username, 0, 255);     
    memset(password, 0, 255); }  
    int userId; 
    char username[255]; 
    char password[255]; 
}; 

void writefile( User **users)
{
    FILE *userDB = fopen( "users.bin", "wb"); 
    int k=0; for(k=0;k<MAX_USERS-1;k++){     
        if (users[k]!=NULL) {         
            fwrite((const void*)&users[k]->userId, sizeof(int), 1, userDB);         
            fwrite((const void*)users[k]->username, 255, 1, userDB);         
            fwrite((const void*)users[k]->password, 255, 1, userDB);     
            fprintf(stdout, "UserID %d\n", users[k]->userId);
            fprintf(stdout, "%s\n", users[k]->username);
            fprintf(stdout, "%s\n", users[k]->password);
            fflush(stdout);
        } else {         
            fpos_t skip = 255 + 255 + sizeof(int);         
            fsetpos( userDB, &skip);     
        } 
    }  fclose( userDB); 
} 

int _tmain(int argc, _TCHAR* argv[])
{
    User *users[MAX_USERS] = {};
    memset(users, 0, MAX_USERS);
    users[0] = new User();
    users[0]->userId = 1;
    memcpy(users[0]->username,"BLah", strlen("BLah"));
    memcpy(users[0]->password,"something:", strlen("something:"));
    users[2] = new User();
    users[2]->userId = 1;
    memcpy(users[2]->username,"BLah2", strlen("BLah2"));
    memcpy(users[2]->password,"something:", strlen("something:"));
    writefile(users);

    return 0;
}

Are you initializing all variables ? 您是否正在初始化所有变量?
The suspicion is that the array is the cause. 怀疑是数组是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM