# How do I load a file name from string

This is a discussion on How do I load a file name from string within the C++ Programming forums, part of the General Programming Boards category; I am trying to use ifstream and I have the file location "c:\text.txt" saved into a string text and want ...

1. ## How do I load a file name from string

I am trying to use ifstream and I have the file location
"c:\\text.txt" saved into a string text and want to have ifstream load the file loaction and name from that string

is that possible?

2. I think you have the wrong forum. ifstream is C++.

In C:
Code:
FILE *fp;
char str[] = "text.txt";
fp = fopen(str,"r");

//do stuff

fclose(fp);

3. In C++:

Code:
ifstream i("c:\\test.txt");
string s;
i >> s;
ifstream file(s);
I think that will work.

4. >>I am trying to use ifstream
I guess you want the C++ forum then! Moving thread...

5. s.c_str() you mean right?

6. I know this part

ifstream a_file ("c:\\text.txt");

a_file>>str;

cout<<str;

but instead of having "c:\\text.txt" I need to replace it with

string result;

so it would be like this

ifstream a_file (result);
a_file>>str;
cout<<str;

if that explains it better

thanks for the help so far

7. Code:
string filename;
string info;
cout << "Enter file name!  : ";
getline(cin, filename, '\n');
ifstream in(filename.c_str());
in >> info;
//or
getline(in, info, '\n);
in.close();
cout << info << endl;

8. thanks ILoveVectors that is exatley what I was looking for.

thanks for every one elses help to.