Openning a file while in an object

D

DJ

I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.

Thanks for any help.

David
 
J

John Harrison

DJ said:
I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream
in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.

No there is nothing wrong with the code (although I could suggest an
improvement or two). Your file must be failing to open for all the usual
reasons. Mostly likely being that the file doesn't exist.

The following program runs without any error message for me (in.txt exists
of course).

#include <iostream>
#include <fstream>
using namespace std;

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

int main()
{
myclass f("in.txt");
}

john
 
D

DJ

Thank you,
i dont know what is going on but since i installed .net 2003, i have been
having a lot of wierd errors. I closed the editor a bit ago and when i
openeed it again it ran ok. And now i can't get strings to work, so i
thought ah well maybe i should just reinstall version 6 of Visual Studio,
but i cant seem to load it over .NET, so im forced to dealwith BAD C++
instead of real c++.

See my latest post

Subject:Trouble with Strings

David
 
J

John Harrison

DJ said:
Thank you,
i dont know what is going on but since i installed .net 2003, i have been
having a lot of wierd errors. I closed the editor a bit ago and when i
openeed it again it ran ok. And now i can't get strings to work, so i
thought ah well maybe i should just reinstall version 6 of Visual Studio,
but i cant seem to load it over .NET, so im forced to dealwith BAD C++
instead of real c++.

I use .NET, never had any problems with it. Are you sure you are picking up
the correct header files?

john
 
S

Stuart Gerchick

DJ said:
I have been attempting to move some code that i have into an object from my
main program. Everything is working fine until i try to open an ifstream in
the object. since i want the file access across the object i have declared
it as follows:

class myclass{
private:
ifstream infile;
public:
myclass(string sourcepath);
};

myclass::myclass(string sourcepath)
{
infile.open(sourcepath.c_str(), ios::in); //this causes an error.
if (!infile) cout << "ERROR" << endl;
};

/////////////////////////////////////////////////////////////////
elsewhere....in main()

myclass f("in.txt");

//////////////////////////////////////////////////////////////////

output for this instance of the myclass will cause:

ERROR

Is there something i am missing? Does it need to be static or something?
Like i said the code worked in the main program just fine.

Thanks for any help.

David

While I think there could be some minor changes made to your code, it
looks correct to me and even works. Are you running this in the same
place as you ran the original program
 
D

DJ

Well the 2002 version didnt give me any problems but the 2003 has a bit and
2005 crashes for me, so there is the hierarchy:
6 is stable
2002 works but is limited,
2003 is inconsistant
2005 crashes but looks promissing
 
J

John Harrison

DJ said:
Well the 2002 version didnt give me any problems but the 2003 has a bit
and
2005 crashes for me, so there is the hierarchy:
6 is stable
2002 works but is limited,
2003 is inconsistant
2005 crashes but looks promissing

I've used 2002 and 2003, no problems at all.

Have you tried using the debugger to find out exactly what was going wrong?

On the face of it I would blame your installation. This is pretty basic
stuff that isn't working for you.

john
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top