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++中使用string类时,文件打开函数产生的错误,并提供了将string转换为C字符串的解决方案,以正确打开文件。
7072

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



