file stream errors

For this portion of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void openFiles(ifstream& fileIn, ofstream& fileOut, string cityName)
{
    string dataFile;
    cout << "Please enter the pathway for the file containing city data:" << endl;
    getline(cin, dataFile);
//error points here
    fileIn.open(dataFile);
    if (fileIn.fail())
    {
        cout << "File failed to open" << endl;
        exit (1);
    }

    string outputFile = "C:[my pathway]" + cityName + " output.txt";
//error points here
    fileOut.open(outputFile);
    if (fileOut.fail())
    {
        cout << "File failed to open" << endl;
        exit (2);
    }

    cout << "Files opened" << endl;
}


I'm getting this error, and a corresponding one for the ofstream, at the locations I marked in the code.

error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'|

What's the problem? I'm pretty sure I'm exactly following the examples we were given in class, so I'm quite confused. Thanks in advance.
Last edited on
closed account (Dy7SLyTq)
file.open(dataFile.c_str());
fstream will only open files with char* string names and string.c_str() returns the char* version of the string value
Is this something that only happens on some IDEs? My prof's examples never use the .c_str(). From my notes:

1
2
3
4
ifstream myIn;
string fileName;
fileName = "[pathway]";
myIn.open (fileName);


Why does this work for her and not me ??

Also, do you think you could explain what char* means? I tried searching it but haven't gotten any meaningful results.

Thanks
ifstream's constructor and open() member function accept strings in modern implementations (this behavior was standardized in C++11).
Earlier, they only accepted pointers to elements of null-terminated arrays of char (aka C strings)
Last edited on
Ok, I made the modification and the program compiles, but I'm having a problem when it runs. When it gets to this part:

1
2
3
4
5
6
7
8
9
10
11
12
void openFiles(ifstream& fileIn, ofstream& fileOut, string cityName)
{
    string dataFile;
    cout << "Please enter the pathway for the file containing city data:" << endl;
    getline(cin, dataFile);

    fileIn.open(dataFile.c_str());
    if (fileIn.fail())
    {
        cout << "File failed to open" << endl;
        exit (1);
    }


it outputs the prompt to enter the pathway and then jumps straight to "file failed to open" and exits without letting me input anything. Why would it be doing this?
closed account (Dy7SLyTq)
@cubbi: thank you i did not know that
for ur problem: there is something wrong with the file. could it be to big? or the wrong file? as for char*'s:
in c, they dont have classes, and as a result, no std::string class. instead they used char*'s, which was literally a string of chars. so its just the precursor to the string class (or i think basic_string<char>)
DTS - the program outputs this without letting me input a filepath (it skips the getline):


Please enter the pathway for the file containing city data:
File failed to open

Process returned 1 (0x1)   execution time : 14.408 s
Press any key to continue.
Last edited on
closed account (Dy7SLyTq)
hmmm... what are you passing to fileIn?
I'm just supposed to give it the pathway for a textfile. but it doesn't let me enter anything...no idea why.
closed account (3qX21hU5)
The code you posted should work perfectly fine and it should not be skipping the getline statement.

Also the main reason people get Failed to open errors is because they don't have the file in the programs directory so make sure you have it there.

Another suggestion for your function would be this. Since it is a function and you are accepting streams, you don't know if that stream is already open or if that stream is in a error state. Both of these can cause major problems and can be a pain to track down and fix.

So I recommend you do something like this at the very beginning of the function just to be sure.

1
2
3
4
    fileIn.close();
    fileIn.clear();
    fileOut.close();
    fileOut.clear();


If any of your two streams have a file already opened it will close them and it also clears any errors state the two streams might have. Always remember to do something like this when using a stream with multiple files cause it will save you from a big headache later on.
Topic archived. No new replies allowed.