How can I open string-named files?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
string I;

cout << "Decide the file name: ";
cin >> I;

ifstream inFile (I);

[other things...]

return 0;
}

here it says me:
error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot c
onvert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

...? thanks

i think you mean this
1
2
3
4
5
6
7
string I;

cout << "Decide the file name: ";
cin >> I;

ifstream inFile((I+".txt").c_str());
I should write for example:
Decide the file name: input.txt

and it should open the file..
(and in the same folder I already have a file input.txt)

oh ok so I only had to add .c_str()
Thanks :)
Last edited on
you only need to type
input because
 
(I+".txt")

ads .txt to whatever you type
you can change .txt if you want;
Last edited on
Topic archived. No new replies allowed.