C++ 如何读写ppm文件?

C++ 如何读写ppm文件?,c++,ppm,C++,Ppm,我尝试读取ppm文件a并创建一个新的文件。但是当我用GIMP2打开它们时,图像是不一样的 我的代码哪里有问题 int main() { FILE *in, *out; in = fopen("parrots.ppm","r"); if( in == NULL ) { std::cout<<"Error.\n"; return 0; } unsigned char *buffer = NULL;

我尝试读取ppm文件a并创建一个新的文件。但是当我用GIMP2打开它们时,图像是不一样的

我的代码哪里有问题

int main()
{
    FILE *in, *out;
    in = fopen("parrots.ppm","r");
    if( in == NULL )
    {
        std::cout<<"Error.\n";
        return 0;
    }

    unsigned char *buffer = NULL;

    long size = 0;
    fseek(in, 0, 2);
    size = ftell(in);
    fseek(in, 0, 0);

    buffer = new unsigned char[size];
    if( buffer == NULL )
    {
        std::cout<<"Error\n";
        return 0;
    }

    if( fread(buffer, size, 1, in) < 0 )
    {
          std::cout<<"Error.\n";
          return 0 ; 
    }

    out = fopen("out.ppm","w");
    if( in == NULL )
    {
         std::cout<<"Error.\n";
         return 0;
    }

    if( fwrite(buffer, size, 1, out) < 0 )
    {
         std::cout<<"Error.\n";
         return 0;
    }

    delete[] buffer;

    fcloseall();

    return 0;
}
intmain()
{
文件*输入,*输出;
in=fopen(“parrots.ppm”,“r”);
if(in==NULL)
{
标准::cout
缺少的包括

FILE *in, *out;
< C++程序中的p> C风格I/O,为什么?也在初始化点声明,接近第一次使用。
in = fopen("parrots.ppm","r");
fseek(in, 0, 2);
这是以文本模式打开文件,这肯定不是您想要的。请使用
“rb”
作为模式。

unsigned char *buffer = NULL;
if( fwrite(buffer, size, 1, out) < 0 )
在初始化时声明,接近第一次使用

in = fopen("parrots.ppm","r");
fseek(in, 0, 2);
您应该使用
SEEK\u END
,但不能保证将其定义为2

参见上文,对于
SEEK\u SET
不保证定义为0

buffer = new unsigned char[size];
if( buffer == NULL )
默认情况下,
new
不会返回
NULL
指针,但会抛出
std::bad_alloc
异常。(在大多数当前操作系统上,过度分配是常态,即使使用
malloc()检查
NULL
也不会保护您不受内存不足的影响)
,但很高兴看到您养成了检查的习惯。)

C++11带给我们的。使用它们。它们是避免内存泄漏的一个极好的工具(C++为数不多的弱点之一)

再次使用文本模式,您希望
在此处显示“wb”

unsigned char *buffer = NULL;
if( fwrite(buffer, size, 1, out) < 0 )
不是标准函数。请使用
fclose(in);
fclose(out);


C++11解决方案(为了简洁起见,省略错误检查)看起来有点像这样:

#include <iostream>
#include <fstream>
#include <memory>

int main()
{
    std::ifstream in( "parrots.ppm", std::ios::binary );
    std::ofstream out( "out.ppm", std::ios::binary );

    in.seekg( 0, std::ios::end );
    auto size = in.tellg();
    in.seekg( 0 );

    std::unique_ptr< char[] > buffer( new char[ size ] );

    in.read( buffer.get(), size );
    out.write( buffer.get(), size );

    in.close();
    out.close();

    return 0;
}
#包括
#包括
#包括
int main()
{
std::ifstream-in(“parrots.ppm”,std::ios::binary);
std::ofstreamout(“out.ppm”,std::ios::binary);
in.seekg(0,std::ios::end);
自动大小=in.tellg();
in.seekg(0);
std::unique_ptr缓冲区(新字符[大小]);
in.read(buffer.get(),size);
out.write(buffer.get(),size);
in.close();
out.close();
返回0;
}

当然,智能解决方案可以通过或(在本文撰写时是实验性的)进行实际的文件系统拷贝。

您是否尝试过使用官方库来实现此目的?-.FYI。
#include <iostream>
#include <fstream>
#include <memory>

int main()
{
    std::ifstream in( "parrots.ppm", std::ios::binary );
    std::ofstream out( "out.ppm", std::ios::binary );

    in.seekg( 0, std::ios::end );
    auto size = in.tellg();
    in.seekg( 0 );

    std::unique_ptr< char[] > buffer( new char[ size ] );

    in.read( buffer.get(), size );
    out.write( buffer.get(), size );

    in.close();
    out.close();

    return 0;
}