Sign in to follow this  
TLAK1001

Can't load vector

Recommended Posts

I'm working on a server/client application where the server must load a list of all clients plus their actual name and password. A currently seperate program writes the data as follows:
 cout<<"Saving..."<<endl;
    ofstream nameFout(NAMEFILE,ios::binary); //Write the name vector
    nameFout.write((char*)(&numOfClients),sizeof(numOfClients)); /*Server needs
                                     to know how many clients are in the list*/
    vectorSize=sizeof(userNames);
    nameFout.write((char*)(&vectorSize),sizeof(vectorSize));
    nameFout.write((char*)(&userNames),vectorSize);
    nameFout.close();
    
    ofstream litFout(LITNAMEFILE,ios::binary); //Write the lit name vector
    vectorSize=sizeof(litNames);
    litFout.write((char*)(&vectorSize),sizeof(vectorSize));
    litFout.write((char*)(&litNames),vectorSize);
    litFout.close();
    
    ofstream passFout(PASSFILE,ios::binary); //Write the password vector
    vectorSize=sizeof(passwords);
    passFout.write((char*)(&vectorSize),sizeof(vectorSize));
    passFout.write((char*)(&passwords),vectorSize);
    passFout.close();

When the server accesses the saved data as follows:
ifstream nameFin(NAMEFILE,ios::binary); //Load the name vector
    nameFin.read((char*)(&numOfClients),sizeof(numOfClients)); /*numOfClients is
                                                           included in this file*/
    nameFin.read((char*)(&vectorSize),sizeof(vectorSize));
    nameFin.read((char*)(&userNames),vectorSize);
    nameFin.close();
    
    ifstream litNameFin(LITNAMEFILE,ios::binary); //Load the litNames vector
    litNameFin.read((char*)(&vectorSize),sizeof(vectorSize));
    litNameFin.read((char*)(&litNames),vectorSize);
    litNameFin.close();
    
    ifstream passFin(PASSFILE,ios::binary); //Load the passwords
    passFin.read((char*)(&vectorSize),sizeof(vectorSize));
    passFin.read((char*)(&passwords),vectorSize);
    passFin.close();
    
    cout<<"DONE"<<endl;

it fails to properly load the vectors. I get an access violation when I do cout<<vector[0]<<endl; What's going on?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this