将字节从 P6 PPM 文件读取到字符数组 (C++)
Reading bytes from a P6 PPM file into a character array (C++)
以下是相关代码:
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;值是 ?
。等。
看起来您希望它打印 BYTE 值,而不是字符。试试这个:
cout << "i is " << i << "; value is " << (int)(temp[i]) << endl;
通过将字符转换为整数,cout 将打印值,而不是 ASCII 代码。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 按字符值访问int数组
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- C++ 带结构的数组字符
- 数组字符包含量超出预期
- 读取文件并添加到数组字符
- 如何在Qt中更改或替换数组字符
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 井字棋数组字符错误
- 分割数组字符