Openning a file while in an object

Discussion in 'C++' started by DJ, Oct 25, 2004.

  1. DJ

    DJ Guest

    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
     
    DJ, Oct 25, 2004
    #1
    1. Advertising

  2. "DJ" <> wrote in message
    news:iw%ed.6489$...
    >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
     
    John Harrison, Oct 25, 2004
    #2
    1. Advertising

  3. DJ

    DJ Guest

    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

    "John Harrison" <> wrote in message
    news:...
    >
    > "DJ" <> wrote in message
    > news:iw%ed.6489$...
    > >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
    >
    >
     
    DJ, Oct 25, 2004
    #3
  4. "DJ" <> wrote in message
    news:S%2fd.1680$...
    > 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
     
    John Harrison, Oct 25, 2004
    #4
  5. "DJ" <> wrote in message news:<iw%ed.6489$>...
    > 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
     
    Stuart Gerchick, Oct 25, 2004
    #5
  6. DJ

    DJ Guest

    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

    "John Harrison" <> wrote in message
    news:...
    >
    > "DJ" <> wrote in message
    > news:S%2fd.1680$...
    > > 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
    >
    >
     
    DJ, Oct 27, 2004
    #6
  7. "DJ" <> wrote in message
    news:HTAfd.3376$...
    > 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
     
    John Harrison, Oct 27, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mel
    Replies:
    5
    Views:
    502
    Jan Harders
    Jul 18, 2004
  2. Replies:
    1
    Views:
    290
    Alf P. Steinbach
    Apr 17, 2006
  3. Replies:
    2
    Views:
    278
    Alf P. Steinbach
    Apr 17, 2006
  4. ER
    Replies:
    2
    Views:
    499
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 12, 2007
  5. xiao

    openning a file

    xiao, Aug 5, 2008, in forum: C Programming
    Replies:
    29
    Views:
    691
    santosh
    Aug 6, 2008
Loading...

Share This Page