Writing Data from an Array of Classes to a Binary File C++
<p>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 access violation error. The Access violation error occurs in fwrite.c</p>

<p>I am compiling on Visual Studio 2010 running Windows 7.</p>

<p><strong>User Class</strong></p>

<pre><code>class User {
public:
User() {
    memset(username, 0, 255);
    memset(password, 0, 255);
}

int userId;
char username[255];
char password[255];
};
</code></pre>

<p><strong>MAX_USERS Definition</strong></p>

<pre><code>#define MAX_USERS 2048
</code></pre>

<p><strong>User Manager Constructor</strong></p>

<pre><code>UserManager::UserManager() {
memset( users, 0, MAX_USERS);
}
</code></pre>

<p><strong>Shutdown Function</strong></p>

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

fclose( userDB);
}
</code></pre>

<p>The array 'users' is memset to zero at the constructor.</p>
