一个从二进制文件中读取一个字节而 [英] Reading bytes one by one from binary file
问题描述
这是我的问题,我想打开.jpg文件,并写入每个字节与逗号分隔,到另一个.txt文件一个十进制数(0-255)。现在它应该能够重新建立.jpf文件使用txt文件。这是我想做到这一点。
this is my question, i want to open a .jpg file and write each byte as a decimal number (0-255) separated with a comma, into another .txt file. now it should be able to build the .jpf file again using that txt file. this is how i tried to do it.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
long x;
char *s;
ifstream ifs("image.jpg",ios::binary);
ifs.seekg(0,ios::end);
x=ifs.tellg();
ifs.seekg(0,ios::beg);
s=new char[x];
ifs.read(s,x);
ifs.close();
ofstream is("image.txt");
for(int i=0;i<x;i++){
is<<(unsigned int)s[i]<<",";
}
现在这个节目及牡丹image.txt与十进制数如下,
4294967295,4294967256,4294967295,4294967264,0,16,74,70,73,70,0,1,......
这里有些数字似乎是4字节长,S [I]是指只有一个字节,所以如何能(INT)S [I]返回大量超过255请一些可以一个可以帮助我这个....谢谢..上
now this program creats image.txt with decimal numbers as follows, 4294967295,4294967256,4294967295,4294967264,0,16,74,70,73,70,0,1,...... here some numbers seems to be 4bytes long, s[i] refers only one byte, so how can (int)s[i] return a large number than 255. please can some one help me on this.... thanks..
推荐答案
看来你的机器字符
上的签署的。所以,当你施放一个负数 unsigned int类型
,你会得到一个很大的价值。重新$ P $使用字符
psenting他们时,在输出的大值是负值。请注意,当字符
是签署的,其值可 -128
到 127
不过的字节的可以是 255 $ C之间的
0
$ C>。因此,大于任何值 127
将成为范围之间的负 -128〜-1
。
It seems on your machine char
is signed. So when you cast a negative number to unsigned int
, you get a big value. The big values in the output are negative values when representing them using char
. Note that when char
is signed, its value can be -128
to 127
but a byte can be between 0
to 255
. So any value greater than 127
would become negative between the range -128 to -1
.
使用 unsigned char型
为:
unsigned char *s;
还是做到这一点:
Or do this:
is<< static_cast<unsigned int> (static_cast<unsigned char>(s[i]) )<<",";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
casting to unsigned char first
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
then casting to unsigned int
也就是说,第一个字符
转换为 unsigned char型
,然后无符号整型
。
那么这是所有关于你所遇到的问题。现在,在风格和成语的一些注意事项。在C ++中,你应该避免使用新
尽可能。你的情况,你可以使用的std ::矢量
为:
Well that is all about the issue you're facing. Now some notes on style and idioms. In C++, you should avoid using new
as much as possible. In your case, you can use std::vector
as:
//define file stream object, and open the file
std::ifstream file("image.jpg",ios::binary);
//prepare iterator pairs to iterate the file content!
std::istream_iterator<unsigned char> begin(file), end;
//reading the file content using the iterator!
std::vector<unsigned char> buffer(begin,end);
最后一行从该文件中的所有数据读入缓存
。现在,你可以把它们打印为:
The last line reads all the data from the file into buffer
. Now you can print them as:
std::copy(buffer.begin(),
buffer.end(),
std::ostream_iterator<unsigned int>(std::cout, ","));
有关所有这些工作,你需要在除了你已经在你的code已经添加以下标题:
For all these to work, you need to include the following headers in addition to what you have already added in your code:
#include <vector> //for vector
#include <iterator> //for std::istream_iterator and std::ostream_iterator
#include <algorithm> //for std::copy
正如你可以看到,这个地道的解决方案不使用的指针和新
,它也不使用的投!
这篇关于一个从二进制文件中读取一个字节而的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!