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;

}
4

1 回答 1

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'.)

于 2013-02-11T22:57:55.717 回答