0

I'm opening file in 1 function and trying to use pointer of that in other function. But i dunno why its not working. Below is the Code.

void ReadFile()
    {
        float data;
        int total_rows, pairs;
        double longitude, latitude;

        {
            GsmFingreprintEuc *g;
            ll.push_front(new GsmFingreprintEuc);

            if(file_ptr.is_open())
                cout<<"Yes!!"<<endl;
            else
                cout<<"NO!!"<<endl;
            file_ptr >> data;
            total_rows = data;
            cout<<"Total Rows:"<<total_rows<<endl;

            for (int i = 0; i < total_rows; i++)
            {
                g = ll.front();
                file_ptr >> data;
                pairs = data;
                for (int j = 0; j < pairs; j++)
                {
                    int id;
                    double value;
                    file_ptr >> data;
                    id = data;
                    file_ptr >> data;
                    value = data;
                    g->add_map(id, value);

                }
                file_ptr >> data;
                latitude = data;
                g->set_latitude(latitude);
                file_ptr >> data;
                longitude = data;
                g->set_longitude(longitude);

            }

        }

        cout<<"Size: "<<ll.size()<<endl;

    }

    DtFileReaderEuc(string file_path)
    {
        cout << "I am in Constructor" << endl;
        cout << file_path << endl;
        fstream file_ptr(file_path.c_str(), std::ios_base::in);
        if (file_ptr.is_open()) {
            cout << "Yahhy!! file Opend successfully" << endl;

            float data;
            file_ptr >> data;
            double total_rows = data;
            cout<<"Total Rows:"<<total_rows<<endl;


            //file_ptr = myfile;
            ReadFile();
            //myfile.close();

        } else
            cout << "Wohoo!! Wrong path" << endl;

        cout << "Done!!" << endl;

    }

};

and when i rund this code output is: "I am in Constructor /home/umar/Desktop/DataFile/dha_dataset.gfp Yahhy!! file Opend successfully Total Rows:7257 NO!! Total Rows:0 Size: 1 Done!!"

Thanks in advance

nvoigt
  • 75,013
  • 26
  • 93
  • 142
OOkhan
  • 297
  • 3
  • 8
  • 20
  • This shouldn't even compile! `file_ptr` (which isn't a pointer by the way) is local to `DtFileReaderEuc`, and cannot be used anywhere else. You could pass it as an argument to `ReadFile`. – BoBTFish Oct 10 '13 at 07:26
  • @BoBTFish but Its Compiling. is there anyway else to do this without passing it as an argument? – OOkhan Oct 10 '13 at 07:28
  • Ok, I can't even start to turn this into something I can compile. Too much of the real code is missing. Please create a [Short, Self Contained, Correct Example](http://sscce.org). Also, read [this](http://kuhllib.com/2012/01/14/stop-excessive-use-of-stdendl/) and [this](http://stackoverflow.com/q/1452721/1171191). – BoBTFish Oct 10 '13 at 07:31
  • At a guess (and nvoigt has already said more or less the same), you have another `file_ptr` somewhere, which is what `ReadFile` sees. You create a local `file_ptr` in `DtFileReaderEuc`, which [*hides*](http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/name_hiding.htm) the other one. You could have a member variable (I recommend some naming convention for member variables so you don't make this sort of error. A lot of people use an `_` at the end of the name), or pass the `fstream` by reference into `ReadFile`, if it doesn't need to be kept around. – BoBTFish Oct 10 '13 at 07:44

4 Answers4

1
fstream file_ptr(file_path.c_str(), std::ios_base::in);

This is a new fstream variable local to your constructor. You probably meant to use the private variable of the same name.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • well want to open file in constructor and want to use that file_ptr in ReadFile() function can you help me with that how to do that ? Thank You. – OOkhan Oct 10 '13 at 07:41
  • @OOkhan Consider having a member `std::ifstream file_`, then open it in the initialiser list: `MyClass(std::string const & filename) : file_(filename) { if (!file_) std::cerr << "Uh oh!\n" }`. – BoBTFish Oct 10 '13 at 07:50
0

Probably, in order to make the code compile you have put a fstream file_ptr somewhere you could see it from ReadFile but you forgot to remove the local copy in DtFileReaderEuc. In this case you use the local version in DtFileReaderEuc and the "global" one in the ReadFile which is not opened. As someone already suggested to you, try pass file_ptr to ReadFile

Rudy Barbieri
  • 389
  • 1
  • 4
  • 16
0

The file_ptr scope is not clear. You have declared and defined the file_ptr in DtFileReaderEuc so you have to pass its pointer to inner function ReadFile, otherwise, declaration of file_ptr should be in outer scope and put the definition in DtFileReaderEuc.

horaman
  • 1
  • 2
-1

create file_ptr a class member and initialize the same in ctor, then it can be used anywhere in the member functions.

To get the file pointer outside class use getter/setter functions.

  • This is not an answer. It should be a comment. (And actually, I have already made the same comment, so please do not add to the noise). – BoBTFish Oct 10 '13 at 07:32