problem using ifstream

Inside one of my functions I am using ifstream and then the getline function to read lines from a data file. My function takes a string from my main program and uses the string to open a file Here is what my code looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int load_DB(string data_base_file)
{
        string line;

       ifstream myfile("data_base_file");

        if (!myfile)
        {
            cout << "Unable to open file!!! " << endl;
            cin.get();
            system("pause");
        }

        getline(myfile, line);
.
.
.


Whenever I compile, my program spits out "unable to open file". If I replace "data_base_file" with the actual file name, it works fine. However, I need to execute the function as described above. Why does the file not open using the this method?

BTW: I am using code::blocks with the g++ complier. I originally wrote this program using Microsoft visual c++. The program works fine in visual c++ but not using the g++ complier. What is wrong?
Last edited on
In the code you posted, ifstream is trying to open a file named "data_base_file".
Is that the actual name of your file?

What I suspect you really want to do is to open the file contained in the argument data_base_file. To do that, you need to pass a C-string to the ifstream constructor.

ifstream myfile(data_base_file.c_str());
Topic archived. No new replies allowed.