D
David Blasdell
This appears very strange to me, I'm having a problem with a fstream object
inside a class which is not being called directly from main (another class
calls the class that contains the fstream object).
Use the following code (stripped down from what I'm actually working on, but
does has the problem I'm experiencing)
Create a file called test.txt in the working directory with a few bytes of
any text or data
Can you explain to me why calling the DataBaseSec::OpenDBFile method via the
DataBase::GetFile method has a problem reading the data of the file where as
calling DataBaseSec::OpenDBFile has no trouble.
Whats even stranger is that if the fstream object is created as a local var
(uncomment the line to test this) it works either way.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//DataBase Section
class DataBaseSec {
private:
fstream file;
public:
bool OpenDBFile(char filename[])
{
char byte=0;
// fstream file; //File reads successfully when fstream object is created
in this method block
//Try both with the above line commented, and without
file.open(filename,ios::in|ios::binary);
if (!file || file.eof())
return false;
file.read((char *) &byte,1);
if (!file || file.eof())
return false; //DataBase::GetFile returns at this point, however a direct
call to DataBaseSec::OpenDBFile continues correctly
return true;
}
};
//Main database object
class DataBase {
private:
vector<DataBaseSec> section;
public:
bool GetFile(char filename[])
{
section.resize(section.size()+1);
if (!section[section.size()-1].OpenDBFile(filename)) {
section.pop_back();
return false;
} else
return true;
}
};
int main()
{
DataBase db;
DataBaseSec dbs;
//Try from main object
cout << "From main object: ";
if (db.GetFile("test.txt"))
cout << "True" << endl;
else
cout << "FALSE" << endl;
//Try directly with section object
cout << "From section object: ";
if (dbs.OpenDBFile("test.txt"))
cout << "True" << endl;
else
cout << "FALSE" << endl;
return 0;
}
inside a class which is not being called directly from main (another class
calls the class that contains the fstream object).
Use the following code (stripped down from what I'm actually working on, but
does has the problem I'm experiencing)
Create a file called test.txt in the working directory with a few bytes of
any text or data
Can you explain to me why calling the DataBaseSec::OpenDBFile method via the
DataBase::GetFile method has a problem reading the data of the file where as
calling DataBaseSec::OpenDBFile has no trouble.
Whats even stranger is that if the fstream object is created as a local var
(uncomment the line to test this) it works either way.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//DataBase Section
class DataBaseSec {
private:
fstream file;
public:
bool OpenDBFile(char filename[])
{
char byte=0;
// fstream file; //File reads successfully when fstream object is created
in this method block
//Try both with the above line commented, and without
file.open(filename,ios::in|ios::binary);
if (!file || file.eof())
return false;
file.read((char *) &byte,1);
if (!file || file.eof())
return false; //DataBase::GetFile returns at this point, however a direct
call to DataBaseSec::OpenDBFile continues correctly
return true;
}
};
//Main database object
class DataBase {
private:
vector<DataBaseSec> section;
public:
bool GetFile(char filename[])
{
section.resize(section.size()+1);
if (!section[section.size()-1].OpenDBFile(filename)) {
section.pop_back();
return false;
} else
return true;
}
};
int main()
{
DataBase db;
DataBaseSec dbs;
//Try from main object
cout << "From main object: ";
if (db.GetFile("test.txt"))
cout << "True" << endl;
else
cout << "FALSE" << endl;
//Try directly with section object
cout << "From section object: ";
if (dbs.OpenDBFile("test.txt"))
cout << "True" << endl;
else
cout << "FALSE" << endl;
return 0;
}