【发布时间】:2016-05-29 21:43:51
【问题描述】:
以下是相关代码:
string s;
int width, height, max;
// read header
ifstream infile( "file.ppm" ); // open input file
infile >> s; // store "P6"
infile >> width >> height >> max; // store width and height of image
infile.get(); // ignore garbage before bytes start
// read RGBs
int size = width*height*3;
char * temp = new char[size]; // create the array for the byte values to go into
infile.read(temp, size); // fill the array
// print for debugging
int i = 0;
while (i < size) {
cout << "i is " << i << "; value is " << temp[i] << endl;
i++;
}
然后,我得到的输出表明数组中的值要么是空白,要么是“?”。我猜这意味着字节没有正确转换为字符?
i 为 0;值是
i 是 1;值是
i 是 2;值是
i 是 3;值是?
i 是 4;值是?
...等等。
【问题讨论】:
-
ASCII 编码不会为 0 到 255 之间的所有值分配可打印字符。
-
"...字节没有转换成字符..."什么意思?
-
@πάνταῥεῖ 我不知道......我真的很困惑。我希望字节在数组中表示为 0 到 255 之间的字符。
-
@user 所以你想看看数字吗?重复的问题可以很好地为您提供所需的答案。
-
重要提示:您必须以
ios::binary模式打开文件才能安全读取二进制数据。如果不这样做,在 Windows 平台上,由于 CR LF 到 LF 的转换,二进制序列 13 10 将被读取为 10,从而导致颜色分量丢失。