- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
今晚我一直在做噩梦,试图让一些非常简单的 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;
}
此代码处于“错误修复模式”,因此非常困惑,不要太担心,当此方法最终起作用时会进行清理。
我试过:
我现在有点不知所措,我真的不确定为什么这个文件拒绝加载。
条件:if (!getline(m_ifs, line))
重复返回 true... :(
编辑:我刚刚尝试在打开后立即检查 m_ifs.fail()
并返回 true,因此我们知道失败标志已被触发:/
谢谢
家伙
最佳答案
在打开流之前启用异常:
m_ifs.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
否则 m_ifs.open
不会抛出。
而且你必须捕获std::ifstream::failure
:
try {
m_ifs.open("C:\\Users\\guyth\\Documents\\test.txt", fstream::in);
}
catch (std::ifstream::failure e) {
std::cerr << "Exception opening file: " << std::strerror(errno) << "\n";
}
参见 ios::exceptions
了解更多详情。
关于c++ - ifstream.open() 不打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39213989/
根据 C++11 标准,是 的行为 ifstream in("."); 指定的,还是系统相关的? 对于上下文,我试图避免使用 boost::filesystem 和类似的库,因为它们会导致不相关的可移
我尝试使用 ifstream 读取一个 3GB 的数据文件,但它给出了错误的文件大小,而当我读取一个 600MB 的文件时,它给出了正确的结果。除了错误的文件大小,我也无法使用 ifstream 读取
这里有没有人知道 C++ ifstream 的 get 指针在调用 read() 后可能会损坏的方法?我看到了一些我无法解释的真正奇怪的行为。例如(说明性代码,而不是我实际运行的代码): int ma
我正在编写一个外部合并排序。它是这样工作的:从大文件中读取 k 个 block ,在内存中对它们进行排序,执行 k 路合并,完成。所以我需要在 k-way 合并阶段从文件的不同部分顺序读取。最好的方法
阅读 Savitch 的 Problem Solving in C++,std::ifstream::fail 以检查文件是否已正确打开(ifstream 或 ofstream)。 正如我第一次看到的
我正在尝试逐行读取多个文件(本例中为 3 个)并使用 ifstream shared_ptrs vector 来执行此操作。但我不知道如何取消引用此指针以使用 getline() 或我的代码中存在其他
我正在编写一个程序,它从一个文本文件中获取多个变量。当程序发现EOF时, 它结束输入数据。 int main() { int val, count = 0; ifstream file
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想知道在什么情况下我们可以拥有: bool(std::ifstream) != std::ifstream::good() 区别在于 bool(std::ifstream) 不测试 eof 位,而
也许是个伪问题,但我需要一个明确的答案。这些函数的返回有什么不同吗 int FileExists(const std::string& filename) { ifstream file(file
我只是采用了一些在 Linux 下开发的文件阅读器的旧代码,并尝试在我的 Windows 项目中使用完全相同的代码,该项目是用 MSVC++7.1 编译的。代码编译没有任何问题,但根据 Windows
是var > var相同? 据我所知,它们应该完全相同。但是已经很晚了,我的大脑处于半睡状态,所以我想澄清一下。 最佳答案 它们不一样。 foo > foo是bar.operator>>(foo)或
我一直在测试使用 Visual Studio 在 C++ 中读取文件的性能,我得到了一些我真的不明白的结果。 我的代码如下: std::vector Method1(std::string fileP
我使用 SymbolHound 在网络上查找比较两者的资源,但找不到任何东西。 在 VS13 中寻找 std::ifstream::in 的声明和定义让我找到了 basic_ifstream。寻找 s
我无法将图像文件读入缓冲区。当我读取普通的 ascii 文件时,一切都很好,但当涉及到图像文件时,我怀疑图像文件中有一个\0 字符? 我需要接收图像文件,将其解析为 16KB 的 block 以进行哈
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
首先,我为我的英语表示歉意。 我在尝试编写一个 XML 解析器时遇到了一个奇怪的问题。为了解释我的问题,我应该说,我有一个 xml 解析器类,它有一个 ifstream 成员。这个类有一个函数,它会读
我正在使用 xcode。我编译得很好,然后一旦它运行它就成功打开文件,然后读取两个值,但这些值不会进入变量,因此它会跳过 for 循环并关闭文件并从 main 返回。 #include #inclu
我有一个包含以下格式数据的文件: name1 p1 p2 ... p11 name2 p1 p2 ... p11 ... (参数不一定在一行) 我的目标是读取名称和 11 个参数,对它们做一些事情,然
我正在尝试读取一个输入文件,该文件的格式为一行中有两个数字,并将该行中的第一个数字存储在一个 vector 中,将第二个数字存储在另一个 vector 中。 除了实际读取文件之外,我的代码的每个部分都
我是一名优秀的程序员,十分优秀!