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)
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?
@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.
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.
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.