Sep 12, 2010 at 9:34pm UTC
In a new and supposedly improved version of a scheduler program I am working on, I have encountered a problem: I need to open a file specified by the user. How can I get ifstream to accept a string variable as a filename?
Sep 12, 2010 at 10:34pm UTC
Mayby not, when I use "ifs" for the ifstream and "file_name" for the file name it works fine, but when I change them around it doesn't.
Example:
string month;
string data [31];
cout << "Please enter a month" << endl;
cin >> month;
ifstream input(month.c_str());
for (int i = 1; i < 31; i++)
{
getline(input, data[i]);
cout << i;
cout << data[i] << endl;
}
input.close ( );
What am I doing wrong?
Also, can you use this same code for ofstream, or does that require something compleatly diffrent?
Last edited on Sep 12, 2010 at 10:38pm UTC
Sep 13, 2010 at 1:16am UTC
That looks like it should work. As long as you only type in a one word name when it asks you for month. Can you explain what exactly is going wrong?
And yes, its the same for ofstream;
Sep 13, 2010 at 2:00am UTC
Found the problem: I'm an idiot.
forgot to include the fstream library.
Everything works fine now.