简体   繁体   中英

C++ Windows compiled program different behaviour to Linux

I wrote some code for a Linux system to read/write bitmap files.

I transfered this code to Windows, and attempted to compile it with Visual Studio 2019.

I tested my program by opening a bitmap file and saving a copy of it. I found that only the first few lines of the bitmap image were written to file and the rest of the file was blank. (Black image color.)

I was extremely confused by this so wrote an even simpler program which just dumps out the integer values of bytes in the file.

I ran this on a 32 x 32 bitmap image file, where the file was saved from GIMP, with 24 bits per pixel format (R8 G8 B8), and "Do not write colorspace information" set in compatiablity options.

I found that the below code prints data up to about i = 500 , and then it prints zeros for the rest of the file.

The file looks like this:

城市32x32

It clearly does not contain large regions of zeros.

Edit: I edited the below code to add an output file. The screenshot below the code shows the output produced.

#include <iostream>
#include <fstream>
#include <vector>


int main()
{
    
    std::ifstream ifile("city32.bmp");

    ifile.seekg(0, std::ios::end);
    std::size_t fsize = ifile.tellg();
    std::cout << "fsize=" << fsize << std::endl;

    ifile.seekg(0, std::ios::beg);
    std::vector<unsigned char> buf(fsize, 0);
    std::cout << "vector size: " << buf.size() << std::endl;

    ifile.read((char*)&buf[0], fsize);

    for(int i = 0; i < buf.size(); ++ i)
    {
        std::cout << i << "." << (int)buf.at(i) << "  ";
    }
    std::cout << std::endl;

    std::ofstream ofile("city32_out.bmp");
    ofile.write((char*)&buf[0], fsize);
    ofile.close();

    ifile.close();

    return 0;

}

捕获

Link to original image, if interested: https://allhdwallpapers.com/wp-content/uploads/2015/06/Shanghai-1.jpg

Make sure you open your file in binary mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM