pointer to ifstream as a static member

Fist time working with input in c++.
I'm trying to emulate something I do in java when I need to set a file in one place and read from it in a few others.
What I would do is make scanner (file input) a static variable and call it wherever I needed it.

This is my current code:
.h
1
2
3
4
5
6
7
8
9
10
11
using namespace std;

class Train;
class ifstream;
class Station {
public:
    static void addArivalFromFile();
    static void runSim();
private:
    static ifstream* myfile; 
};

.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Station.h"
#include "Train.h"

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
void Station::runSim()
{
    myfile = new ifstream("test.txt" , ifstream::in) ;
    test = new Train(1,3);
    test->getArival();
    if (myfile->is_open())
    {
        cout << "Starting stuff...";    
    }
    else
    {
        //TODO: DIE!!!
    }
    
    
    //End Value
    myfile->close();

}

void Station::addArivalFromFile()
{
    string line; 
    if(myfile->good())
    {
        getline (*myfile,line);
        //Do stuff with the line
    }
}


Is there a better way to do what i'm doing here?
Thanks
Replace the pointer to ifstream with an object of type ifstream,
1
2
3
4
5
6
7
8
9
10
11
using namespace std;

class Train;
class ifstream;
class Station {
public:
    static void addArivalFromFile();
    static void runSim();
private:
    static ifstream myfile; // *** 
};


This will avoid the resource leak in Station::runSim(), which would now become:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Station::runSim()
{
    myfile.open("test.txt" ) ;
    test = new Train(1,3);
    test->getArival();
    if (myfile)
    {
        cout << "Starting stuff...";    
    }
    else
    {
        //TODO: DIE!!!
    }
    
    
    //End Value
    myfile.close();

}


This construct
1
2
3
4
5
6
    string line; 
    if(myfile->good())
    {
        getline (*myfile,line);
        //Do stuff with the line
    }

results in a subtle error; the check for failure of getline() is made before getline() is called.

With the pointer gone, this would now become:
1
2
3
4
5
6
7
8
void Station::addArivalFromFile()
{
    string line; 
    if( getline( myfile, line) )
    {
        //Do stuff with the line
    }
}

c++ is not java.
1
2
3
4
void Station::runSim()
{
    myfile = new ifstream("test.txt" , ifstream::in) ; //memory leak
    test = new Train(1,3); //there are better ways, like Train test; 
I don't really get what you want, as you are closing the file. ¿why don't just read all what you need the first time?
Also, keep in mind that you could make several 'Station' objects, ¿are you sure that all of them should look at the same file?

If you just want serialization then Station::serialization(istream &) will do fine. You could overlod the insertion operator '<<' too.

@JLBorges
Thanks that pointed me in the right direction.
There was one hang up. I was getting an error when I hit the linker and I just blindly wrote the line
ifstream Station::myfile;
I don't know what it is but it works. I'd guess its because I never created an object for myfile. It's going to take a bit to get in the swing of c++.
Thanks again

@ne555
The Station class is entirely static.
The reason I need to be able to pull input from another method is because I'm running a long simulation and populating the timeline with an entire file from the start would be too much for the hardware i'm using. So instead I only pull more input when I need it.
> I don't know what it is but it works.

You did right. A static member variable of a class in C++ is semantically equivalent to the same in Java. However, C++ makes a distinction between declaring something and defining it.
myfile is just declared to be a static member variable in the class; ifstream Station::myfile; defines it.

Incidentally, do not put the definition in the header file; place it in an implementation file (ideally in Station.cxx). C++ also has an ODR - http://en.wikipedia.org/wiki/One_Definition_Rule - and a header may be included in multiple translation units.


Thanks that was a huge help!
Topic archived. No new replies allowed.