将P6 PPM文件中的字节读入字符数组(C ++)

时间:2016-02-17 22:51:52

标签: c++ file-io io ppm

以下是相关代码:

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++;
}

然后,我得到的输出表明数组中的值是空白或“?”。我想这意味着字节没有正确转换成字符?

  我是0;价值是

     我是1;价值是

     我是2岁;价值是

     我是3岁;价值是?

     我是4岁;价值是?

...等。

1 个答案:

答案 0 :(得分:1)

看起来您希望它打印BYTE值,而不是字符。试试这个:

cout << "i is " << i << "; value is " << (int)(temp[i]) << endl;

通过将char转换为int, cout 将打印值,而不是ASCII码。

相关问题