如何故意触发 fgets() 中的错误?
How do I intentionally trigger an error in fgets()?
摘要
使用fgets()
时,我有错误检查代码,可以在fgets()
返回null
但尚未到达文件末尾的情况下执行一些修复。我想练习这部分代码来验证它是否按预期工作。
有没有一种规范的方法可以触发fgets()
失败?要么通过手动方式(以某种方式删除fgets()
调用之间的文件),一些测试设置(提供故意"损坏"的文件),还是其他什么?
最小可重现性示例
fgets_fail_test.cpp:
// Test code to show minimally reproducible example of fgets() error handling
// The desire is to manually trigger a failure in fgets()
#include <errno.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp = fopen("test_file.txt", "r"); //assume this works, not testing this
char buf[100]; //assume large enough to hold data
unsigned int lineno = 1;
while (fgets(buf, 100, fp)) {
std::cout << buf;
lineno++;
}
if (feof(fp)) {
std::cout << "End of file encountered.n";
}
else if (ferror(fp)) { // how do I trigger an fgets error to cause this to return true?
printf("Encountered read error at line no %u. Error: %d, %s.n",
lineno,
errno,
strerror(errno));
}
fclose(fp); // assume this works, not testing this
return 0;
}
test_file.txt:
ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ
The quick brown fox
jumped over the
lazy dog.
Goodbye.
理论
虽然我可以简单地用一些测试脚手架替换有问题的代码(编写一个保持内部状态的 fgets 包装器,增加一个计数器,一旦它到达第 #N 行,它就会返回null
并手动设置文件错误和 errno)我觉得应该有一些"内置"方法来做到这一点?
如果唯一的解决方案是脚手架,我会这样做。老实说,也许我在这里试图变得太聪明了。
在一些相关和链接的问题中给出了很多很好的答案,这可能会使我的问题重复。虽然我在进行初始搜索时专注于fgets()
,所以我无法找到这些。
一些相关问题是:
- 导致 I/O 错误的特殊文件
- 生成读取错误
由于我的程序从具有给定文件名格式的已知文件列表中读取数据,因此我需要使用一种允许我向程序提供错误文件的方法(而无需修改代码)。我决定用这个答案来做到这一点。
方法
为了生成一个会触发fgets()
读取失败的错误文件,我利用了/proc/self/mem
的"功能",即从中读取会导致I/O错误。
我删除了旧的输入文件,并将旧名称下的新输入文件链接到/proc/self/mem
:
ln -s /proc/self/mem test_file.txt
然后运行该文件生成以下输出:
Encountered read error at line no 1. Error: 5, Input/output error.
指示我们触发了错误。感谢@Artyer的评论,他们最初将我链接到相关问题。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 如何故意触发 fgets() 中的错误?
- 在visual c++中的fgets()上接收访问冲突错误
- "Sams teach yourself C" 中的示例 使用"fgets"但返回错误