2011-01-09 79 views

回答

8

是的,当然是的。

const char * filename = "abc.txt"; 

std::ifstream fin(filename); 

还是用的std :: string

std::string filename = "abc.txt"; 

std::ifstream fin(filename.c_str()); 

用C++ 11,你可以直接使用字符串。

std::ifstream fin(filename); 
-1

给文件名与文件扩展

字符串VAR = “的text.txt””

ifstream.open(var.c_str(),IOS ::二进制);

+0

C++中没有String类。即使你的意思是std :: string,ifstream.open也不会将字符串作为第一个参数。 – Yacoby 2011-01-09 16:15:41

+0

@Yacoby我知道了! ..即时通讯abt stf ::字符串 – Sudantha 2011-01-09 16:16:46

1

是的;你将它作为参数传递给构造函数。

3

你也可以使用一个字符数组(这基本上是一个字符串是什么反正):

char filename[20]; 

std::cout << "Enter the filename (no more than 20 characters): "; 
std::cin >> filename; 

std::ifstream inputFile(filename); 

这应该工作,并允许您利用动态用户输入您的文件名。