【问题标题】:Cannot open a file using ifstream in C++ [duplicate]无法在 C++ 中使用 ifstream 打开文件 [重复]
【发布时间】:2016-04-05 23:01:40
【问题描述】:

我是编程新手,这是我的第二个项目。 我按照讲师的说明编写了这些以使用 ifstream 打开文件,但总是出现错误说 错误:没有匹配函数调用 'std::basic_ifstream::open(std::string&)'|

字符串文件名,命令; ifstream myfile;

cout << "Enter the file name: ";
cin >> filename;
myfile.open(filename);
while(myfile >> numloops)
{
    while(numloops != 0)
    {

部分代码

【问题讨论】:

  • 你是否也在代码中添加文件扩展名?
  • 存在完整的错误,不,这不是链接错误。它也与文件扩展名无关。

标签: c++


【解决方案1】:

您正在使用 C++03 编译器。

只有从 C++11 开始,std::string 才能用于为std::fstream 构造函数或std::fstream::open() 提供文件名。

由于历史原因,旧版本的标准需要 C 风格的字符串。

你可以使用std::string::c_str()获得一个,所以:

myfile.open(filename.c_str());

不过,理想情况下,您应该改用非古代编译器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多