dirk/C++/c++/print-newline
Weingarten, 10-02-18
print-newline – Suppress Conversion of '\n' to CRLF
This is rather a MS Windows specific problem (may be Apple OSX also?)
I had to know to setup an output stream which prints '\n' as is.
The Story Behind
A co-worker of mine is a great SGI Irix fan.
Linux is OK, but he rather hates MS Windows.
For his luck, he's a scientist in a reputated research institute what allows the access to the necessary, rather exotic hardware as well as the total avoidance of any MS driven system.
His main work for our company is the development of a simulation system.
Of course, it is available for Irix and Linux.
The input files have ASCII format which allows editing in a usual text editor.
(That's not intentional but really helpful in emergency cases.)
However, the software detects CR characters ('\r') in the input file scanner and (instead of simply ignoring them) throws a respective error message.
This may sound funny but leads periodically to awesome support calls when appearing on customer side.
I'm a little bit more liberal about the choice of the OS.
Thus, I believe in portable software whereby concentrating on Linux and MS Windows with option for more.
However, due to the attitude of my co-worker, it's essential for me that certain tools I develop for this software have to support Unix line endings on any system.
Source Code
print-newline.cc |
/** @file sample for output of newlines
*
* This is especially relevant if a text file is written on a MS Windows
* system which is dedicated to be processed on a *ix system.
*
* Last CVS checkin:
* $Date: $
* $Author: scheff $
*/
/**************************************************************************/
// standard C/C++ header:
#include <fstream>
#include <iostream>
#include <string>
/**************************************************************************/
using namespace std;
/** waits for user confirm.
*
* @param action text with next action
*/
static void pause(const char *action)
{
cerr << "Press [ENTER] to " << action << ": " << flush;
string str; getline(cin, str);
}
/** provides the main function of application.
*
* @param argc number of command line arguments
* @param argv pointer to array of pointers to strings with command line
* arguments
* @return 0 ... application exited regularly\n
* else ... execution of application aborted
*/
int main(int argc, char *argv[])
{
pause("start");
cout << "cout << endl:" << endl;
cout << "cout << \"\\n\":\n";
cout << "cout.put('\\n'):"; cout.put('\n');
cout << "cout.write():"; char c = '\n'; cout.write(&c, sizeof c);
pause("write file 'test'");
{ ofstream fout("test");
fout << "fout << endl:" << endl;
fout << "fout << \"\\n\":\n";
fout << "fout.put('\\n'):"; fout.put('\n');
fout << "fout.write():"; char c = '\n'; fout.write(&c, sizeof c);
}
pause("write file 'test-bin'");
{ ofstream fout("test-bin", ios::out | ios::binary);
fout << "fout << endl:" << endl;
fout << "fout << \"\\n\":\n";
fout << "fout.put('\\n'):"; fout.put('\n');
fout << "fout.write():"; char c = '\n'; fout.write(&c, sizeof c);
}
// done
pause("finish");
return 0;
}
|
I compiled and started the sample on Linux to check for portability:
> g++ -o print-newline print-newline.cc↵
>
|
Output
This was a test run on MS Windows XP:
| Console (hex-dump): |
00000000: 63 6f 75 74.20 3c 3c 20 : 65 6e 64 6c.3a 0d 0a 63 cout << endl:..c
00000010: 6f 75 74 20.3c 3c 20 22 : 5c 6e 22 3a.0d 0a 63 6f out << "\n":..co
00000020: 75 74 2e 70.75 74 28 27 : 5c 6e 27 29.3a 0d 0a 63 ut.put('\n'):..c
00000030: 6f 75 74 2e.77 72 69 74 : 65 28 29 3a.0d 0a out.write():..
|
| File 'test' (hex-dump): |
00000000: 66 6f 75 74.20 3c 3c 20 : 65 6e 64 6c.3a 0d 0a 66 fout << endl:..f
00000010: 6f 75 74 20.3c 3c 20 22 : 5c 6e 22 3a.0d 0a 66 6f out << "\n":..fo
00000020: 75 74 2e 70.75 74 28 27 : 5c 6e 27 29.3a 0d 0a 66 ut.put('\n'):..f
00000030: 6f 75 74 2e.77 72 69 74 : 65 28 29 3a.0d 0a out.write():..
|
| File 'test-bin' (hex-dump): |
00000000: 66 6f 75 74.20 3c 3c 20 : 65 6e 64 6c.3a 0a 66 6f fout << endl:.fo
00000010: 75 74 20 3c.3c 20 22 5c : 6e 22 3a 0a.66 6f 75 74 ut << "\n":.fout
00000020: 2e 70 75 74.28 27 5c 6e : 27 29 3a 0a.66 6f 75 74 .put('\n'):.fout
00000030: 2e 77 72 69.74 65 28 29 : 3a 0a .write():.
|
Conclusion
The file output stream (ofstream) supports a binary flag which provides the required behavior.
I'm not quite sure about standard output (cout).
May be, I will consider this another day.
Files
dirk/C++/c++/print-newline
Last modified: Fri Feb 26 11:30:38 2010