今天晚上我一直在做噩梦,试图让一些非常简单的 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)
该代码处于“错误修复模式”,因此非常混乱,不用太担心,当该方法最终起作用时,就会进行清理。
我努力了: