首页 > 解决方案 > 为什么 Visual Studio 无法识别 is_open() 函数

问题描述

#include <fstream>
#include <string>

using namespace std;
void Readfile(string fname)
{
    ifstream infile(fname);
    if (is_open(infile))
    {
        while (!infile.eof())
        {
            string sline = "";
            getline(infile, sline);
        }
        infile.close();

    }
    else
        stderr << "unable to open file" << fname << endl;

}

Visual Studio 说标识符“is_open”是未定义的,即使我包含了 fstream 库。

标签: c++

解决方案


is_open是一种方法std::ifstream。使用infile对象来调用它:

ifstream infile(fname);
if (infile.is_open())
{
   //....
}

推荐阅读