Reputation: 47
I am trying to open a binary database with this code
Store::Store(const char* filename)
{
ifstream inFile;
inFile.open(filename, ios::in | ios::binary);
if(!inFile){
cout << "Could not open file " << data << "!" << endl;
}
while( inFile ){
inFile.read((char*) this, sizeof(Store));
}
inFile.close();
The error I getting is the "Could not open file" error, and I am pretty stumped.
Upvotes: 0
Views: 461
Reputation: 46677
First of all, data
was probably supposed to be filename
.
The likeliest reason is that the working directory of your application is not where you think it is, and the file does not exist. Try with an absolute path to check if this is the case.
Not related to the question: this way of 'saving' the object is very unsafe and highly dependent on the way how your compiler arranges Store
instances in memory. The saved stores are most likely not compatible with versions of the program compiled with other compilers, and potentially not even with different builds from the same compiler.
If Store
has virtual members, you are practically guaranteed to crash. Technically, it is undefined behaviour anyway.
You should look into serialising not the whole objects, but only the raw data parts of your database. A look into boost.serialization
may be helpful.
Upvotes: 4
Reputation: 941
Your code appears correct for accessing the file, I would make sure the file path is correct especially if it is a relative path. You might be in the wrong working directory. Also you may not possess the require permissions to read the file, try running with admin/root privileges (which usually isn't the case with reading a file, but worth mentioning).
I would print out the working directory and the file name to make sure everything is being referenced correctly.
Upvotes: 2