读取PPM文件头(类型P6),宽度缺失
Reading PPM file header (type P6), width is missing
假设您正在阅读这样的PPM文件:
ifstream img;
img.open("Image.ppm", ios::binary);
try
{
if (img.fail())
{
throw("Can't open input file");
}
string header;
int w, h, b;
img >> header;
cout << header << endl;
if (header.compare("P6") != 0)
{
throw("Wrong format file. File needs to be P6 type!");
}
img >> w >> h >> b;
cout << w << " " << h << " " << b << endl;
if (b < 0 || b > 255)
{
throw("An error message");
}
img.ignore(256, 'n');
}
catch (const char *err)
{
fprintf(stderr, "%sn", err);
img.close();
}
有人删除了标题中的宽度或高度值。现在b值将以字节形式读取RGB数字。是否存在if语句不会阻止程序结束的可能情况?换句话说,是否存在一种优化的方法来防止此类错误?
一个可能的问题:1. b之后的数据是二进制的,所以您的场景中的b可以是非ascii的,因此可以为0,这可以通过错误消息的测试。
在最简单的情况下,如果R值恰好是一个数字的ASCII值(48-57),G值恰好是空格的ASCII值(32),那么b
将以该数字的值结束(因此不会通过测试)
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 将字节从 P6 PPM 文件读取到字符数组 (C++)
- 读取PPM文件头(类型P6),宽度缺失