小编Cat*_*x16的帖子

ifstream.open() 未打开文件

今天晚上我一直在做噩梦,试图让一些非常简单的 I/O 功能运行起来。尽管很尴尬,但这里的人们给了我很大的帮助!

我当前的问题是我正在尝试使用 ifstream.open() 但它根本没有打开文件。getline(ifstream,line) 证实了这一点;第一次调用时返回 false。

这是当前代码的复制粘贴:

std::string FSXController::readLine(int offset, FileLookupFlag flag)
{
    // Storage Buffer
    string line;
    streampos sPos(offset);

    try
    {
        // Init stream
        if (!m_ifs.is_open())
            m_ifs.open("C:\\Users\\guyth\\Documents\\test.txt", fstream::in);
    }
    catch (int errorCode)
    {
        showException(errorCode);
        return "";
    }

    // Set stream to read input line
    m_ifs.seekg(sPos);
    if (!getline(m_ifs, line))
        return "";

    // Close stream if no multiple selection required
    if (flag == FileLookupFlag::single)
        m_ifs.close();

    return line;

}
Run Code Online (Sandbox Code Playgroud)

该代码处于“错误修复模式”,因此非常混乱,不用太担心,当该方法最终起作用时,就会进行清理。

我努力了:

  • 绝对文件路径
  • 将路径保存到字符串中,然后调用 .c_str() 方法。
  • 在管理员模式下运行 VS 2015
  • 确保文件具有读/写访问权限
  • 确保没有重复的文件扩展名 …

c++ file istream

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

file ×1

istream ×1