0

I'm writing a simple program that transforms a file from its original type into a binary form.

However, I'm having the following problem. The code compiles correctly but when i run it, the console window opens and never closes until i close it myself. Also, i have noticed that the longer the console window stays open, the larger the size of my newly created binary file will be. Below is my code:

#include <fstream>//to open a file

using namespace std;
int main(void){
   ifstream in("in.JPG");
   ofstream out("out.bin", ios::binary);
   double d;

   while(!in.eof()) {
      out.write((char*)&d, sizeof d);
   }
   out.close();
   in.close();

return 0;

}
Chris Condy
  • 596
  • 1
  • 9
  • 24

1 Answers1

4

You never read anything from in, therefore it has no chance of ever hitting eof.

Also, using eof in loop conditions is likely to lead to incorrect results, as explained in the following SO question: Why is iostream::eof inside a loop condition considered wrong?

(Generally, it's unclear what you're trying to do. .jpg is a 'binary' format, there is no sensible notion of 'converting a .jpg file to binary'.)

Community
  • 1
  • 1
us2012
  • 15,457
  • 2
  • 41
  • 61