我应该使用什么I/O函数来编写.bmp文件?

时间:2022-09-10 22:44:53

The example code I've seen for this seems to use standard C file output functions, but I'd like to make it in C++.

我看到的示例代码似乎使用了标准的C文件输出函数,但我希望使用c++。

I tried using fsteam functions to do it, but no data is written to the .bmp file at all.

我尝试使用fsteam函数来实现这个功能,但是没有任何数据被写入到.bmp文件中。

So far, I have tried the standard <<, put, and write, and none of these work. If I open it up with a hex editor, the file is still empty.

到目前为止,我已经尝试了标准<< <、put和write,但这些都没有。如果我用十六进制编辑器打开它,文件仍然是空的。

It's odd, since the input functions work fine.

它是奇数,因为输入函数工作得很好。

Here's a piece of the code I used to test to see if it was working:

下面是我用来测试它是否正常工作的代码:

output.open("WHITE.bmp");
output.put('B'); // this doesn't seem to work, the file is empty when I open it in a hex editor. 
output.put('M');

And the rest of the code:

剩下的代码:

#include <iostream>
#include <fstream>

using namespace std;
typedef unsigned char  byte;
typedef unsigned short dbyte;

struct BMPINFO
{
    int width;
    int height;
};

int main()
{
    ifstream sourcefile;
    ofstream output;

    int threshold = 150;



    sourcefile.open("RED.bmp");


    if(sourcefile.fail())
    {
        cout << "Could not open RED.bmp" << endl;
        return 1;
    }


    if(sourcefile.get() == 'B')
    {
        if(sourcefile.get() == 'M')
        {
            cout << "RED.bmp is a valid .bmp file" << endl;
        }
    }

    else
    {
        cout << "RED.bmp is not a valid .bmp file" << endl;
        return 1;
    }

    BMPINFO image;

    // seeks to bitmap width, this file is little end in.

    sourcefile.seekg (0x12, ios::beg);

    unsigned int i = (unsigned)sourcefile.get(); 
    i += (unsigned)sourcefile.get() << 8;


    image.width = i;

    cout << "The width of the image is: " << image.width << endl;

    sourcefile.seekg (0x16, ios::beg);

    i = sourcefile.get(); 
    i += (unsigned)sourcefile.get() << 8;

    image.height = i;


    cout << "The height of the image is: " << image.height << endl;

    int loc_pixels;

    sourcefile.seekg (0x0A, ios::beg);

    loc_pixels = sourcefile.get();

    cout << "Location of pixel array is: " << loc_pixels << endl;


    output.open("WHITE.bmp");

    output.put('B'); // this doesn't seem to work, the file is empty when I open it in a hex editor. 
    output.put('M');

    if(output.bad())
    {
        cout << "the attempt to output didn't work" << endl;
        return 1;
    }


    sourcefile.seekg(loc_pixels, ios::beg);

    char data[30000];

    output.close();


    return 0;
}

Is there a special function I should be using to output to this .bmp file?

是否有一个特殊的函数我应该用来输出到这个。bmp文件?

EDIT - added more code, though most of it doesn't have to do with file output

编辑——添加了更多的代码,尽管大部分代码与文件输出无关

2 个解决方案

#1


4  

You have a buffer overflow bug in this code:

在这个代码中有一个缓冲区溢出错误:

char data[30000];    // Prepare file for usage -- just copy one thing from the file to the other
sourcefile.read(data, image.height * image.width );

You are reading in image.height*image.width bytes, and trying to fit them into 30000 bytes. You should structure your code so that those two numbers are related.

您正在读取图像。高度*图像。宽度字节,并试图将它们放入30000字节。您应该构造代码,使这两个数字相互关联。

Try this:

试试这个:

std::vector<char> data(image.height * image.width);
sourcefile.read(&data[0], data.size());

#2


1  

There's a great description here.

这里有一个很好的描述。

ofstream myfile;
myfile.open("WHITE.bmp", ios::out | ios::binary); // opening in binary mode
myfile << 'B';
myfile << 'M';
myfile.close();

#1


4  

You have a buffer overflow bug in this code:

在这个代码中有一个缓冲区溢出错误:

char data[30000];    // Prepare file for usage -- just copy one thing from the file to the other
sourcefile.read(data, image.height * image.width );

You are reading in image.height*image.width bytes, and trying to fit them into 30000 bytes. You should structure your code so that those two numbers are related.

您正在读取图像。高度*图像。宽度字节,并试图将它们放入30000字节。您应该构造代码,使这两个数字相互关联。

Try this:

试试这个:

std::vector<char> data(image.height * image.width);
sourcefile.read(&data[0], data.size());

#2


1  

There's a great description here.

这里有一个很好的描述。

ofstream myfile;
myfile.open("WHITE.bmp", ios::out | ios::binary); // opening in binary mode
myfile << 'B';
myfile << 'M';
myfile.close();