Had*_*ekh 3 c++ fstream ifstream ofstream
我正在尝试编写简单的c ++代码来读写文件.问题是我的输出文件小于原始文件,我找不到原因.我有一个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;
}
Run Code Online (Sandbox Code Playgroud)
为什么不呢:
#include <fstream>
int main()
{
std::ifstream mySource("im1.jpg", std::ios::binary);
std::ofstream myOutpue("im2.jpg", std::ios::binary);
myOutpue << mySource.rdbuf();
}
Run Code Online (Sandbox Code Playgroud)
或者,不那么喋喋不休:
int main()
{
std::ofstream("im2.jpg", std::ios::binary)
<< std::ifstream("im1.jpg", std::ios::binary).rdbuf();
}
Run Code Online (Sandbox Code Playgroud)