Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary open and copy image file c++

Tags:

c++

file

I want to copy one image file to another new file. This is my method to do this:

    std::ofstream myOutpue;
    std::ifstream mySource;
    //int i = 0;
    mySource.open(ofn.lpstrFile, std::ios::binary);
    myOutpue.open("im4.jpg", std::ios::binary);
    char buffer;
    char bufferToSave[100];
    if (mySource.is_open())
    {
        //client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
        i++;
        while (!mySource.eof())
        {
            mySource >> std::noskipws >> buffer;
            myOutpue << buffer;
            //client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
            i++;
        }
    }
    i++;
    //client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
    mySource.close();
    //myOutpue.close();

This method work correctly, but my problem is that i want to copy char/bit's and send it to another client. When i doing this by each char , that not work correctly so i want to make a bigger buffor(for example char t[512]) or something like that and copy them to new file.

I try to doing this like that:

    std::ofstream myOutpue;
    std::ifstream mySource;
    mySource.open(ofn.lpstrFile, std::ios::binary);
    myOutpue.open("im4.jpg", std::ios::binary);
    char buffer;
    char bufferToSave[100];
    if (mySource.is_open())
    {
        //client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
        i++;
        while (!mySource.eof())
        {
            if (i == 100)
            {
                for (int i = 0; i < 100; i++)myOutpue << bufferToSave[i];
                i = 0;
            }
            mySource >> std::noskipws >> buffer;
            bufferToSave[i] = buffer;
            //myOutpue << buffer;
            //client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
            i++;
        }
    }
    i++;
    //client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
    mySource.close();
    myOutpue.close();

But i get image that i can't open.

So my question is how to read file to get more bits from it and that create me the same image as original.

like image 324
dawcza94 Avatar asked Nov 22 '25 22:11

dawcza94


1 Answers

You have an error in your original file copy algorithm in that you should never loop using eof() as the end flag.

See: Why is iostream::eof inside a loop condition considered wrong?

Copying files can be a simple as this:

    std::ofstream("output.jpg", std::ios::binary) << std::ifstream("input.jpg", std::ios::binary).rdbuf();

It uses a special overload of the output operator when passing an std::istream buffer (using rdbuf()). It copies the whole stream.

When reading a whole buffer you should use std::istream::read:

    std::ifstream ifs("input.jpg", std::ios::binary)

    char buffer[1025]; // create a buffer

    // keep going as long as the reading succeeds
    while(ifs.read(buffer, sizeof(buffer)))
    {
        // ifs.gcount() is the number of chars read successfully
        client->sendFilePacket(buffer, ifs.gcount()); // send all bytes
    }
like image 169
Galik Avatar answered Nov 24 '25 10:11

Galik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!