C++ PNM阅读器:Can';不能以二进制模式读取某些文件

C++ PNM阅读器:Can';不能以二进制模式读取某些文件,c++,visual-c++,visual-studio-2012,c++11,binaryfiles,C++,Visual C++,Visual Studio 2012,C++11,Binaryfiles,我在使用pnm文件时遇到了一些问题(这是很明显的,否则我不会在这里发布XD)。问题是,我的老师让我们开发一个简单的二进制模式的pnm阅读器,然后把它打印到屏幕上。我正在使用libEGL(一个可用的框架)。我的问题是,它只适用于这两个图像,而不适用于任何其他图像 对于birch.pnm和checkers.pnm,它可以工作,但cathedral.pnm、cotton.pnm和fish_tile.pnm只是简单地进入无限循环或抛出错误 这些图像是可用的 我的代码如下: #include <io

我在使用pnm文件时遇到了一些问题(这是很明显的,否则我不会在这里发布XD)。问题是,我的老师让我们开发一个简单的二进制模式的pnm阅读器,然后把它打印到屏幕上。我正在使用libEGL(一个可用的框架)。我的问题是,它只适用于这两个图像,而不适用于任何其他图像

对于birch.pnm和checkers.pnm,它可以工作,但cathedral.pnm、cotton.pnm和fish_tile.pnm只是简单地进入无限循环或抛出错误

这些图像是可用的

我的代码如下:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "engcomp_glib.h"

using namespace std;

struct RGB{
    char red, green, blue;
};

int main(int argc, char* argv[]){
    RGB **image;
    RGB pixel;
//ifstream _file("..\\bin\\birch.pnm");
ifstream _file("..\\bin\\checkers.pnm");

//ifstream _file("..\\bin\\cotton.pnm");
//ifstream _file("..\\bin\\cathedral.pnm");
//ifstream _file("..\\bin\\fish_tile.pnm");
string type, maxColor;
int width, height;

if(_file){
    _file >> type;

    if(type != "P6")
        cout << "Error! File type is not allowed." << endl;

    _file >> width >> height >> maxColor;
    _file.close();

    egl_inicializar(width, height, true);
    image = new RGB *[height];

    for(int row = 0; row < height; row++)
        image[row] = new RGB[width];

    //Working 8D
    //_file.open("..\\bin\\birch.pnm", ios::binary);
    _file.open("..\\bin\\checkers.pnm", ios::binary);

    //Not working D:<
    //_file.open("..\\bin\\cathedral.pnm", ios::binary);
    //_file.open("..\\bin\\fish_tile.pnm", ios::binary);
    //_file.open("..\\bin\\cotton.pnm", ios::binary);

        //imagem img; img.carregar("..\\bin\\birch.pnm");

        _file.seekg(0, _file.end);

        int size = _file.tellg();
        int currentSize = 0, counter = 0;
        char byte;

        _file.seekg(0, _file.beg);

        do{
            _file.read(reinterpret_cast<char *> (&byte), sizeof(char));

            if(byte == 10 || byte == 13)
                counter++;

        }while(counter < 3);

        int rows = 0, columns = 0;

        while(size != currentSize){
            _file.read(reinterpret_cast<char *> (&pixel), sizeof(RGB));

            if(rows < height && columns < width){
                image[rows][columns] = pixel;
                rows++;
            }
            else if(rows == height){
                rows = 0;
                columns++;

                image[rows][columns] = pixel;

                rows++;
            }
            //else if(columns >= width)
                //currentSize = size;

            currentSize = _file.tellg();
        }

        _file.close();

        while(!key[SDLK_ESCAPE]){
            for(int row = 0; row < height; row++)
                for(int column = 0; column < width; column++)
                    //egl_pixel(row, column, image[row][column].red, image[row][column].green, image[row][column].blue);
                    egl_pixel(column, row, image[column][row].red, image[column][row].green, image[column][row].blue);
                    //img.desenha(0, 0);
            egl_desenha_frame(false);
        }
    }
    egl_finalizar();

    return 0;
}
#包括
#包括
#包括
#包括
#包括“engcomp_glib.h”
使用名称空间std;
结构RGB{
焦红色、绿色、蓝色;
};
int main(int argc,char*argv[]){
RGB**图像;
RGB像素;
//ifstream_文件(“..\\bin\\birch.pnm”);
ifstream_文件(“..\\bin\\checkers.pnm”);
//ifstream_文件(“..\\bin\\cotton.pnm”);
//ifstream_文件(“..\\bin\\cathedral.pnm”);
//ifstream_文件(“..\\bin\\fish_tile.pnm”);
字符串类型,maxColor;
int宽度、高度;
如果(_文件){
_文件>>类型;
如果(类型!=“P6”)
cout宽度>>高度>>最大颜色;
_file.close();
egl_inicializar(宽度、高度、真实值);
图像=新RGB*[高度];
对于(int row=0;row=宽度)
//当前大小=大小;
currentSize=_file.tellg();
}
_file.close();
而(!键[SDLK\u ESCAPE]){
对于(int row=0;row
这是没有意义的,因为它对他们中的两个都有效,应该对他们所有人都有效 我在一个文本编辑器中打开了它们,它们都有标题,所以问题不存在。我做错了什么?我的同事编写了一个代码,将像素存储到一个大小为[height*width]的数组中,可以读取除cathedral.pnm以外的几乎所有图像


感谢您的耐心和帮助:)

pnm的规范规定,标题中的值用空格分隔,通常是换行符,但它们也可以是空格或制表符(或者我现在想不起来的其他内容;)。例如,cathedral文件有一个空格作为分隔符

根据规范,您从上到下、从左到右读取文件,而不是从左到右、从上到下读取文件

如果你真的想正确,如果maxColor不小于256,你应该读短裤而不是chars

您可以通过以下方式在此处找到规格:

祝你好运