Windows and Unix systems (Linux + MacOS X) treat end-of-line differently.
In particular, if the C++ compiler is configured to process the Unix end-of-line character, and getline() is used to read a text file that uses Windows end-of-line characters, the getline() method will not work as expected. To see why, suppose that a text file contains the lines:
Hello GoodbyeIf this file was created on a Windows system, then an ifstream to it will see:
Hello\015\010Goodbye\015\010However, if it was created on a Unix system, then an ifstream to it will see:
Hello\010Goodbye\010So long as your C++ compiler is configured consistently with one's system, this makes no difference.
That is, if getline() is used to read the first line in the file and the C++ compiler is configured for Windows end-of-line characters and we read the file created on Windows, then getline() will read until it has read the CR and LF characters. and then return the string "Hello". Likewise, if the C++ compiler is configured for Unix end-of-line characters and we read the file created on a Unix system, then getline() will read until it has read the LF character, and then return the string "Hello".
The problem comes when you mix and match. For example, if the C++ compiler is configured for Unix end-of-line characters and we read the file created on a Windows system, then getline() will read until it has read the LF character, and then return the string "Hello\015". If you have a unit test that compares whether this string and "Hello" are equal, that test will fail.
For situations where you will be moving files back and forth between Windows and Unix systems, this is exactly what you want. You just have to be careful that you create your text files on a Unix system, or create them using a Windows system application that produces Unix system end-of-line characters. (See Eclipse below.)
If you will be working exclusively in the Windows
environment, then you can change cygwin's default using
Run it as before, and when you get to this screen:
click the DOS/text radio button, finish the installation, and cygwin will be configured to use Windows end-of-line characters. But you should only do this if you will never be transferring your projects/files to a Unix system.
Window > Preferences > General > Workspacewhere you will see this screen:
Note that by default, new text files use the system default end-of-line character/delimiter.
To change this, click the Other radio button
and then choose Unix from the pull-down menu:
Click the Ok button to save the change, and any text files that you subsequently create will have Unix end-of-line characters.
Note that if you have existing text files that are causing problems, you will need to
Or, if you are using Cygwin and have the dos2unix utility installed, you can use it to convert the file from the command-line:
$ dos2unix -o windowsFile.txt -n unixFile.txtThe corresponding unix2dos utility can be used to perform the opposite conversion.