2013-07-20 43 views
0

有什么方法可以访问? 有文本文件一样john.txt micheal.txt james.txt ....访问变量的文本文件

我可以用这个代码访问它们:

ifstream file1("james.txt", ios::in); 

我可以像打开一个文件?

string name = "james"; ifstream file1(name, ios::in) 
+0

在C++ 03和更早的版本,你需要使用'james.c_str()' ,但原则上,是的。 (在C++ 11中它只会工作。)[我假设你的意思是设置'string name =“james.txt”'。否则,'.txt'将会丢失。] – jogojapan

回答

4

也许你可以尝试这样的:

ifstream file1((name + ".txt").c_str(), ios::in); 
+0

“你也许可以试试”是什么意思?它会起作用吗,还是不行? – jogojapan

+0

它的工作。谢谢 – iyazici

+0

我说“也许你可以试试”,因为这个问题不是很清楚,我不确定是否回答。我很高兴它做到了。 –

-2

试试这个:

char name[]="james.txt"; 
ifstream file1(name, ios::in);