I literally ran out of ideas. All I want is to read and write from and to a binary file into a class that I created.
int ClientData::readClientDat(ClientData * client_block)
{
int clientData_SIZE;
ifstream inClients;
inClients.open("D:\\Programming\\Qt\\bookstore instances\\bookstore_system\\data\\clients.dat", ios::binary);
clientData_SIZE = static_cast<int>(inClients.tellg());
client_block = new ClientData [clientData_SIZE+1];
if (inClients.is_open())
{
for(int i = 0; i <clientData_SIZE; i++)
{
// inClients.read((char *)&client_block[i].user, sizeof(client_block[i].user));
// inClients.read((char *)&client_block[i].pass, sizeof(client_block[i].pass));
// inClients.read((char *)&client_block[i].mail, sizeof(client_block[i].mail));
// inClients.read((char *)&client_block[i].id, sizeof(client_block[i].id));
inClients.read((char *)(&client_block[i]), sizeof(ClientData));
}
inClients.close();
}
return clientData_SIZE;
}
It doesn't read correctly for some reason and I guessed I had something wrong with the writing function
void ClientData::insertNewClient(ClientData newUser)
{
ofstream outClients;
outClients.open("D:\\Programming\\Qt\\bookstore instances\\bookstore_system\\data\\clients.dat", ios::binary | ios::app); // write the new client's data in the database.
int clientData_SIZE = outClients.tellp();
newUser.id = (clientData_SIZE) + 1;
// ClientData * client_block;
// client_block[clientData_SIZE] = newUser;
outClients.write((char *)(&newUser), sizeof(ClientData));
outClients.close(); //-----------------------------------------closes file.
}
What should I do?
Here's my class
class ClientData
{
public:
ClientData();
int readClientDat(ClientData * client_block);
ClientData enter_activeUser(QString QUser, QString QPass, QString QMail);
bool isAdmin(ClientData user);
bool userExists(ClientData newClient);
int isClient(ClientData &user);
void insertNewClient(ClientData newUser);
private:
char user[32];
char pass[32];
char mail[32];
int id;
};
client_block = new ClientData [clientData_SIZE+1];to do?ClientDataand writeuser,pass,mailandidone by one, in the format of your choosing).tellg()immediately after opening your file? 2) And how does the number of bytes in the file translate to the number of records to be read from the file? Hint: both are trick questions.new ClientData[clientData_SIZE+1]part does that, yes (though you will want to check the value ofclientData_SIZE, you'll have a surprise). Now, what does theclient_block =part do? Or, said another way: how do you expect the caller to get that array?