将图像读取为二进制文件
reading an image as a binary file
我在codeproject上读到了关于这个项目的文章。它将图像作为二进制对象读取,然后检查其标头的前10个字节。我写了以下代码在Windows机器上运行:
int main () {
std::ifstream is ("warren.jpg", std::ifstream::binary);
if (is) {
// get length of file:
// is.seekg (0, is.end);
int length = 11;
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... "<<endl;
char c='b';
for(int i=0;i<11;i++)
{
is>>c;
cout<<c<<endl; //this just prints b 10 times
}
// read data as a block:
is.read (buffer,length-1);
buffer[length-1]= ' ';
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
cout<<"data is "<<buffer<<endl;
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
输出为:
Reading 11 characters...
b
b
b
b
b
b
b
b
b
b
b
error: only 0 could be readdata is
所以,我知道第一行
std::ifstream是("warren.jpg",std::ifnstream::binary);
在输入if子句时成功。但在那之后,没有接收到任何作为输入的内容。我知道,由于它是二进制输入,所以不应该使用像is>>c这样的格式化输入。但我只是在是.read()不成功时才写这篇文章。
有人能告诉我问题出在哪里吗?
您将不得不使用以下两个ios::binary | ios::in
标志打开您的文件:
std::ifstream ifs (L"c:\james.rar", std::ios::binary | std::ios::in);
相关文章:
- 如何在 c++ 中将二进制文件读取为字符串
- 从二进制文件读取到数组:前面的任意数字
- 提升二进制文件读取错误不支持的版本
- 无法将二进制文件读取到 std::vector<std::byte> 中C++
- 提升序列化在从二进制文件读取时引发异常
- 从二进制文件 C++ 读取的字符串数组写入
- 将二进制文件读取到 std::vector<bool>
- 将fortran生成的二进制文件读取到C++中的带符号整数数组中
- 将从二进制文件读取的字符串转换为整数
- 从/到C 的二进制文件读取和写作整数
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将 int 的二进制文件读取到字符串 c++
- 将结构从二进制文件读取到链表,导致无限循环
- 将二进制文件读取到向量元素的地址中
- c++从二进制文件读取/写入类
- Python:将二进制文件读取到一个结构中,然后拆包
- 从二进制文件读取/写入十六进制字节
- 简单的 C++ 二进制文件读取
- 从二进制文件 c++ 读取 16 位整数
- 如何优化c++二进制文件读取