C++ 读写二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5420317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading and writing binary file
提问by nf313743
I'm trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCII characters from the first line in the file and nothing else.
我正在尝试编写代码将二进制文件读入缓冲区,然后将缓冲区写入另一个文件。我有以下代码,但缓冲区只存储文件第一行中的几个 ASCII 字符,没有其他任何内容。
int length;
char * buffer;
ifstream is;
is.open ("C:\Final.gif", ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();
FILE *pFile;
pFile = fopen ("C:\myfile.gif", "w");
fwrite (buffer , 1 , sizeof(buffer) , pFile );
回答by Bj?rn Pollex
If you want to do this the C++ way, do it like this:
如果您想以 C++ 方式执行此操作,请执行以下操作:
#include <fstream>
#include <iterator>
#include <algorithm>
int main()
{
std::ifstream input( "C:\Final.gif", std::ios::binary );
std::ofstream output( "C:\myfile.gif", std::ios::binary );
std::copy(
std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>( ),
std::ostreambuf_iterator<char>(output));
}
If you need that data in a buffer to modify it or something, do this:
如果您需要缓冲区中的数据来修改它或其他东西,请执行以下操作:
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream input( "C:\Final.gif", std::ios::binary );
// copies all data into buffer
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
}
回答by Thomas Matthews
Here is a short example, the C++ way using rdbuf
. I got this from the web. I can't find my original source on this:
这是一个简短的示例,C++ 方式使用rdbuf
. 这是我从网上得到的。我找不到我的原始来源:
#include <fstream>
#include <iostream>
int main ()
{
std::ifstream f1 ("C:\me.txt",std::fstream::binary);
std::ofstream f2 ("C:\me2.doc",std::fstream::trunc|std::fstream::binary);
f2<<f1.rdbuf();
return 0;
}
回答by Alexey Sudachen
sizeof(buffer) == sizeof(char*)
Use length instead.
改用长度。
Also, better to use fopen
with "wb
"....
此外,最好fopen
与“ wb
”一起使用......
回答by jcoder
sizeof(buffer) is the size of a pointer on your last line NOT the actual size of the buffer. You need to use "length" that you already established instead
sizeof(buffer) 是最后一行指针的大小,而不是缓冲区的实际大小。您需要使用您已经建立的“长度”
回答by retrodrone
You should pass length into fwrite instead of sizeof(buffer).
您应该将长度传递给 fwrite 而不是 sizeof(buffer)。
回答by Zeta
There is a much simpler way. This does not care if it is binary or text file.
有一个更简单的方法。这并不关心它是二进制文件还是文本文件。
Use noskipws.
使用 noskipws。
char buf[SZ];
ifstream f("file");
int i;
for(i=0; f >> noskipws >> buffer[i]; i++);
ofstream f2("writeto");
for(int j=0; j < i; j++) f2 << noskipws << buffer[j];
回答by iMajetyHK
It can be done with simple commands in the following snippet.
可以使用以下代码段中的简单命令来完成。
Copies the whole file of any size. No size constraint!
复制任意大小的整个文件。没有大小限制!
Just use this. Tested And Working!!
就用这个。测试和工作!
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("source.pdf",ios::binary|ios::in);
ofstream outfile;
outfile.open("temppdf.pdf",ios::binary|ios::out);
int buffer[2];
while(infile.read((char *)&buffer,sizeof(buffer)))
{
outfile.write((char *)&buffer,sizeof(buffer));
}
infile.close();
outfile.close();
return 0;
}
Having a smaller buffer size would be helpful in copying tiny files. Even "char buffer[2]" would do the job.
具有较小的缓冲区大小将有助于复制小文件。甚至“char buffer[2]”也能完成这项工作。