How to convert a text file from Windows to Unix
<p>When converting from Unix to Windows, I get the correct output; however, when going from Windows to Unix I get some strange output. I thought all I had to allow for was the removal of the carriage return, '\r'. This isn't working though. When I open the text file after running the code, I get some strange results, the first line is correct, and then all hell breaks lose.</p>

<pre><code>   int main( )
{
   bool windows = false;
   char source[256];
   char destination[256]; // Allocate the max amount of space for the filenames.

   cout &lt;&lt; "Please enter the name of the source file: ";
   cin &gt;&gt; source;

   ifstream fin( source, ios::binary );
   if ( !fin )          // Check to make sure the source file exists.
   {
      cerr &lt;&lt; "File " &lt;&lt; source &lt;&lt; " not found!";
      getch();
      return 1;
   }//endif

   cout &lt;&lt; "Please enter the name of the destination file: ";
   cin &gt;&gt; destination;

   ifstream fest( destination );
   if ( fest )          // Check to see if the destination file already exists.
   {
      cout &lt;&lt; "The file " &lt;&lt; destination &lt;&lt; " already exists!" &lt;&lt; endl;
      cout &lt;&lt; "If you would like to truncate the data, please enter 'Y', "
           &lt;&lt; "otherwise enter 'N' to quit: ";
      char answer = char( getch() );
      if ( answer == 'n' || answer == 'N' )
      {
         return 1;
      }//endif
   }//endif
   clrscr();            // Clear screen for neatness.

   ofstream fout( destination, ios::binary );
   if ( !fout.good() )  // Check to see if the destination file can be edited.
   {
      cout &lt;&lt; destination &lt;&lt; "could not be opened!" &lt;&lt; endl;
      getch();
      return 1;
   }//endif
                        // Open the destination file in binary mode.
   fout.open( destination, ios::binary );
   char ch = fin.get(); // Set ch to the first char in the source file.
   while ( !fin.eof() )
   {
      if ( ch == '\x0D' ) // If ch is a carriage return, then the source file
      {                   // must be in a windows format.
         windows = true;
      }//endif
      if ( windows == true )
      {
         ch = fin.get();  // Advance ch, so that at the bottom of the loop, the
      }//endif            // carriage return is not coppied into the new file.
      if ( windows == false )
      {
         if ( ch == '\x0A' )    // If the file is in the Unix format..
         {
            fout.put( '\x0D' ); // When a new line is found, output a carriage
         }//endif               // return.
      }//endif

      fout.put( ch );
      ch = fin.get();
   }//endwh
   if ( windows == true )
   {
      fout.put( '\x0A' );
   }//endif
   fout.close();
   fin.close();                 // Close yer files.

   if ( windows == true )       // A little output for user-friendly-ness.
   {
      cout &lt;&lt; "The contents of " &lt;&lt; source &lt;&lt; " have been coppied to "
           &lt;&lt; destination &lt;&lt; " and converted to Unix format." &lt;&lt; endl;
   }else{
      cout &lt;&lt; "The contents of " &lt;&lt; source &lt;&lt; " have been coppied to "
           &lt;&lt; destination &lt;&lt; " and converted to Windows format." &lt;&lt; endl;
   }//endif
   cout &lt;&lt; "Enter any key to quit.." &lt;&lt; endl;
   getch();
   return 0;
}//endmn
</code></pre>
