简体   繁体   English

使用fstream以c ++读写二进制文件

[英]read and write a binary file in c++ with fstream

I'm trying to write simple c++ code to read and write a file. 我正在尝试编写简单的c ++代码来读写文件。 The problem is my output file is smaller than the original file, and I'm stuck finding the cause. 问题是我的输出文件小于原始文件,我找不到原因。 I have a image with 6.6 kb and my output image is about 6.4 kb 我有一个6.6 kb的图像,我的输出图像大约6.4 kb

#include <iostream>
#include <fstream>

using namespace std;

ofstream myOutpue;
ifstream mySource;

int main()
{        

    mySource.open("im1.jpg", ios_base::binary);
    myOutpue.open("im2.jpg", ios_base::out);

    char buffer;

    if (mySource.is_open())
    {
        while (!mySource.eof())
        {
            mySource >> buffer;            
            myOutpue << buffer;
        }
    }

    mySource.close();
    myOutpue.close();    

    return 1;
}

Why not just: 为什么不呢:

#include <fstream>

int main()
{
    std::ifstream mySource("im1.jpg", std::ios::binary);
    std::ofstream myOutpue("im2.jpg", std::ios::binary);
    myOutpue << mySource.rdbuf();
}

Or, less chattily: 或者,不那么喋喋不休:

int main()
{
    std::ofstream("im2.jpg", std::ios::binary)
        << std::ifstream("im1.jpg", std::ios::binary).rdbuf();
}

Two things: You forget to open the output in binary mode, and you can't use the input/output operator >> and << for binary data, except if you use the output operator to write the input-streams basic_streambuf (which you can get using rdbuf ). 两件事:你忘了以二进制模式打开输出,你不能使用输入/输出运算符>><<二进制数据,除非你使用输出运算符写输入流basic_streambuf (你可以使用rdbuf )。

For input use read and for output use write . 对于输入使用read和输出使用write

There are 3 problems in your code: 您的代码中有3个问题:

1- You have not opened your output file in Binary. 1-您尚未在二进制文件中打开输出文件。

2- Your code return "1", normally you should return "0", if something went wrong then return an error code. 2-你的代码返回“1”,通常你应该返回“0”,如果出现问题然后返回错误代码。

3- You should use "manipulators" and make c++ not to avoid whitespaces, so in order to read from file instead of: 3-你应该使用“操纵器”并使c ++不要避免空格,所以为了从文件中读取而不是:

mySource >> buffer;

you should use: 你应该使用:

mySource >> std:noskipws >> buffer;

Well, its just because of padding at the end of the image. 好吧,这只是因为图像末尾的填充。 eof of any file do not include the padded bytes added at the end of file. 任何文件的eof都不包括在文件末尾添加的填充字节。 Try this take img1.jpg contains 20 space charecter at the end not visible here (uegfuyregwfyugwrerycgerfcg6ygerbucykgeugcrgfrgeyf ) and run your program (do not include parenthesis in the file, these are used to show the data content) you will see img2.jpg contains (uegfuyregwfyugwrerycgerfcg6ygerbucykgeugcrgfrgeyf) 试试看,img1.jpg包含20个空格字符,最后在这里看不到(uegfuyregwfyugwrerycgerfcg6ygerbucykgeugcrgfrgeyf)并运行你的程序(不包括文件中的括号,这些用于显示数据内容)你会看到img2.jpg包含(uegfuyregwfyugwrerycgerfcg6ygerbucykgeugcrgfrgeyf )

So, its better option to read the file byte by byte using the filesize which you can get using stat , and run for loop till filesize. 因此,使用可以使用stat获取的文件大小逐字节读取文件的更好选项,并运行循环直到filesize。 Hope this should resolve your problem you mentioned above 希望这可以解决您上面提到的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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