Problem with fstream

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;
}
 
J

John Harrison

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;
}

Are you sure it's not the other way around? Anyway your problems are
related to the fact that you are opening the same file twice and not
closing the file before the second attempt to open it. It works when you
use a local variable because a local variable will be closed automatically
when it goes out of scope.

john
 
J

John Harrison

Are you sure it's not the other way around? Anyway your problems are
related to the fact that you are opening the same file twice and not
closing the file before the second attempt to open it. It works when you
use a local variable because a local variable will be closed
automatically when it goes out of scope.

Actually its probably a bit more complex than that. I guess its
implementation defined whether opening the same file twice should work or
not.

But the strange thing is that your code should not compile. fstream objets
are not copyable yet you have put one in a vector and vectors require
their elements to be copyable. I've tried you code on three different
compilers and all reported the same problem. So either that isn't real
code or you have a very forgiving compiler.

john
 
D

David Blasdell

I'm using VC6, and the code I'm actually using doesn't attempt to open the
file twice (I just did this to show the problem I was having and forgot to
close the file in that example code).

I'll try an alternative method of using the fstream object inside the class,
thanks for your help.

Thanks,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream File i/o 1
fstream Buffers 26
problem with fstream 3
fstream problem 3
fstream 6
fstream tests 3
fstream - write a file 3
Character operations in C++ 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top