读取bmp文件数据并将其写入另一个bmp

时间:2017-09-23 11:51:53

标签: c++

我编写了一个代码,我试图读取一个bmp文件并将其写入另一个文件。当我尝试编写它时,输出文件会创建,但它不会打开。这是我的代码

#include<iostream>
#include<fstream>

using namespace std;
//int writeFile(string content);
int main() {

ifstream myReadFile;
ofstream myWriteFile;
 myReadFile.open("D:/MIT_Database/barbara_gray.bmp");
 myWriteFile.open("D:/MIT_Database/barbara_graywrite.bmp");
  char output[100];
 string content;
 if (myReadFile.is_open()) {
 while (!myReadFile.eof()) {
getline( myReadFile, content );
 cout<<content;
 // myReadFile >> output;
   for(int i=0;i<content.length();i++)
{myWriteFile<<content[i];}
 //  myWriteFile<<content<<'\n';
  myWriteFile<<'\n';
  }

}

 myReadFile.close();
 myWriteFile.close();
 return 0;

}

这是我的档案enter image description here

1 个答案:

答案 0 :(得分:1)

fstream in(&#34; test.bmp&#34;,ios :: binary | ios :: in);

fstream out(&#34; new.bmp&#34;,ios :: binary | ios :: out);

char c;

while(!in.eof()){

C = in.get();

out.put(C); }

in.close();

out.close();