string filename = "1.txt";
ifstream fin;
fin.open(filename);
上述语句会产生如下错误:
error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
原因是C++的string类无法作为open的参数。
解决方案:使用C的字符串。
例:
char filename[10];
strcpy(filename, "1.txt");
ifstream fin;
fin.open(filename);
本文介绍了一个关于C++中ifstream.open()函数使用的常见错误,并提供了如何将string类型的文件名转换为C风格字符串的解决方案。
7061

被折叠的 条评论
为什么被折叠?



