Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 写入PGM文件_C++_Pgm - Fatal编程技术网

C++ 写入PGM文件

C++ 写入PGM文件,c++,pgm,C++,Pgm,我正在尝试使用此代码编写pgm文件 myfile << "P5" << endl; myfile << sizeColumn << " " << sizeRow << endl; myfile << Q << endl; myfile.write( reinterpret_cast<char *>(image), (sizeRow*sizeColumn)*sizeof(unsigne

我正在尝试使用此代码编写pgm文件

myfile << "P5" << endl;
 myfile << sizeColumn << " " << sizeRow << endl;
 myfile << Q << endl;
 myfile.write( reinterpret_cast<char *>(image), (sizeRow*sizeColumn)*sizeof(unsigned char));

myfile确保使用
ios::binary
标志以二进制模式打开文件。如果您使用的是Windows,则可能需要将
endl
替换为
“\r\n”

您可能不想使用,因为它会刷新输出流

此外,如果您希望与Windows(可能还有来自Microsoft的任何其他操作系统)兼容,则必须以二进制模式打开该文件。默认情况下,Microsoft以文本模式打开文件,这通常有一个不兼容的功能(古老的DOS向后兼容),没有人想要了:它将每个“\n”替换为“\r\n”

PGM文件格式标头为:

"P5"                           + at least one whitespace (\n, \r, \t, space)
width (ascii decimal)          + at least one whitespace (\n, \r, \t, space) 
height (ascii decimal)         + at least one whitespace (\n, \r, \t, space) 
max gray value (ascii decimal) + EXACTLY ONE whitespace (\n, \r, \t, space) 
这是一个将pgm输出到文件的示例:

#include <fstream>
const unsigned char* bitmap[MAXHEIGHT] = …;// pointers to each pixel row
{
    std::ofstream f("test.pgm",std::ios_base::out
                              |std::ios_base::binary
                              |std::ios_base::trunc
                   );

    int maxColorValue = 255;
    f << "P5\n" << width << " " << height << "\n" << maxColorValue << "\n";
    // std::endl == "\n" + std::flush
    // we do not want std::flush here.

    for(int i=0;i<height;++i)
        f.write( reinterpret_cast<const char*>(bitmap[i]), width );

    if(wannaFlush)
        f << std::flush;
} // block scope closes file, which flushes anyway.
#包括
常量无符号字符*位图[MAXHEIGHT]=…;//指向每个像素行的指针
{
流f的std::of(“test.pgm”,std::ios_base::out
|std::ios_base::binary
|std::ios_base::trunc
);
int maxColorValue=255;

f是你没有工作的东西?如果是,怎么做?