小编use*_*007的帖子

读/写PPM图像文件C++

尝试以我知道的方式读取和写入PPM图像文件(.ppm):

std::istream& operator >>(std::istream &inputStream, PPMObject &other)
{
    inputStream.seekg(0, ios::end);
    int size = inputStream.tellg();
    inputStream.seekg(0, ios::beg);

    other.m_Ptr = new char[size];


    while (inputStream >> other.m_Ptr >> other.width >> other.height >> other.maxColVal)
    {
        other.magicNum = (string) other.m_Ptr;
    }

    return inputStream;
}
Run Code Online (Sandbox Code Playgroud)

我的值对应于实际文件.所以我乐意尝试写数据:

std::ostream& operator <<(std::ostream &outputStream, const PPMObject &other)
{
    outputStream << "P6"     << " "
        << other.width       << " "
        << other.height      << " "
        << other.maxColVal   << " "
       ;

    outputStream << other.m_Ptr;

    return outputStream;
}
Run Code Online (Sandbox Code Playgroud)

我确保使用std :: ios :: …

c++ ifstream ofstream ppm

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

ifstream ×1

ofstream ×1

ppm ×1