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. When UserManager::Shutdown() is called it save the Array of Users to a file. Yet I get an ac开发者_如何学运维cess violation error. The Access violation error occurs in fwrite.c
I am compiling on Visual Studio 2010 running Windows 7.
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
#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
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.
Theory 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.
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.
精彩评论