Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:54:42+00:00 2026-06-01T16:54:42+00:00

When converting from Unix to Windows, I get the correct output; however, when going

  • 0

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.

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

   cout << "Please enter the name of the source file: ";
   cin >> source;

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

   cout << "Please enter the name of the destination file: ";
   cin >> destination;

   ifstream fest( destination );
   if ( fest )          // Check to see if the destination file already exists.
   {
      cout << "The file " << destination << " already exists!" << endl;
      cout << "If you would like to truncate the data, please enter 'Y', "
           << "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 << destination << "could not be opened!" << 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 << "The contents of " << source << " have been coppied to "
           << destination << " and converted to Unix format." << endl;
   }else{
      cout << "The contents of " << source << " have been coppied to "
           << destination << " and converted to Windows format." << endl;
   }//endif
   cout << "Enter any key to quit.." << endl;
   getch();
   return 0;
}//endmn
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:54:43+00:00Added an answer on June 1, 2026 at 4:54 pm

    *If* you only need to convert simple ascii (and perhaps utf-8) text files, you could read the source file line-by-line in a loop in translated mode (handles newlines for you enough for this case) with non-member getline() and then output the lines to the output file while inserting \n or \r\n after each line except the last.

    Then, you can remove the original file and rename the temp file to have the original file’s name. Or, if you want, you can instead push_back the lines into a vector<string>. Then, you could close the input handle to the file, do ofstream out(“filename”, ios_base::trunc) and write the elements of the vector to the file while separating them by the newlines you want.

    It all depends on your requirements.

    The following is an example with minimal error handling. But, it’s really only the FOR loop and reading line-by-line that I want to show here as a different way of doing things.

    convert_file.exe “test.txt” “linux”

    convert_file.exe “test.txt” “win”

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <ostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    
    int main(int argc, char* argv[]) {
        if (argc != 3) {
            cerr << "Usage: this.exe file_to_convert newline_format(\"linux\" or \"win\")" << endl;
            return EXIT_FAILURE;
        }
        string fmt(argv[2]);
        if (fmt != "linux" && fmt != "win") {
            cerr << "Invalid newline format specified" << endl;
            return EXIT_FAILURE;
        }
        ifstream in(argv[1]);
        if (!in) {
            cerr << "Error reading test.txt" << endl;
            return EXIT_FAILURE;
        }
        string tmp(argv[1]);
        tmp += "converted";
        ofstream out(tmp.c_str(), ios_base::binary);
        if (!out) {
            cerr << "Error writing " << tmp << endl;
            return EXIT_FAILURE;
        }
        bool first = true;
        for (string line; getline(in, line); ) {
            if (!first) {
                if (fmt == "linux") {
                    out << "\n";
                } else {
                    out << "\r\n";
                }
            }
            out << line;
            first = false;
        }
        in.close();
        out.close();
        if (remove(argv[1]) != 0) {
            cerr << "Error deleting " << argv[1] << endl;
            return EXIT_FAILURE;
        }
        if (rename(tmp.c_str(), argv[1]) != 0) {
            cerr << "Error renaming " << tmp << " to " << argv[1] << endl;
            return EXIT_FAILURE;
        }
    }
    

    As others have said though, there are already utilities (including text editors like Notepadd++) that do newline conversion for you. So, you don’t need to implement anything yourself unless you’re doing this for other reasons (you didn’t specify).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am manually converting code from Java (1.6) to C# and finding some difficulty
I'm converting database from Teradata to SqlServer. I've noticed all tables and procedures are
I'm converting from C# this LINQ expression. However, it does not seem to work.
I know when converting from nvarchar to varchar , some data will be lost/changed.
I'm looking for options for connecting to (primarily reading data) UNIX/AIX/Business Basic from Windows
I am soon going to be converting my part of an application from using
What's the most most ruby-like way of converting from an Array like [:one, 1,
I'm trying to teach myself MVC3. Im converting from webforms. I need to create
There are two classes: A and B . There are algorithms for converting from
I'm currently building a converter in C#. My program is converting from/to Decimal, Binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.