0

I have written a code where I tried to read a bmp file and write it to another file.When I try to write it an output file creates but it does not open . here is my code

#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;

}

here is my fileenter image description here

USERRR5
  • 430
  • 2
  • 10
  • 27
  • 3
    As you are on Windows, you might want to use the [CopyFile](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx) function provided by the system. – Bo Persson Sep 23 '17 at 11:57
  • How do you know the output file doesn't open? You don't appear to be checking that. – aschepler Sep 23 '17 at 12:14
  • Accessing the file one byte at the time is slow. If `CopyFile` is not an option then see this answer https://stackoverflow.com/a/10195497/4603670 for copying binary files in C++. – Barmak Shemirani Oct 01 '17 at 08:47

1 Answers1

1

fstream in("test.bmp",ios::binary|ios::in);

fstream out("new.bmp",ios::binary|ios::out);

char c;

while(!in.eof()) {

c=in.get();

out.put(c); }

in.close();

out.close();

Michael Popovich
  • 301
  • 1
  • 10