1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Endl and /n not working in g++?

  1. Sep 5, 2012 #1
    For a homework I was supposed to create a program in c++ (has to work in g++ to output some ascii art in a txt file. It works perfectly in Visual Studios, but when I compile g++ using cygwin I just get a single line.
  2. jcsd
  3. Sep 5, 2012 #2


    Staff: Mentor

    What does one of the lines you're displaying look like?

    My thought is that a newline character in VS isn't the same as what you need in cygwin. Windows and Unix/Linux work differently in this regard, with Windows using <CR> + <LF> (ASCII x0A and x0D) and Unix/Linux using just one of these characters.
  4. Sep 6, 2012 #3

    I like Serena

    User Avatar
    Homework Helper

    Welcome to PF, crazyshark!

    Did you inspect your file with notepad?
    Try wordpad instead.
    Then you'll see it's not all on one line.

    As Mark said: it's about different line endings.
    When you install cygwin, you can choose the type of line ending.
    You probably accepted the default, which is Linux-style.

    You can also force the issue by outputting a"\r" before the "\n".
  5. Nov 7, 2012 #4
    I agree with the above poster that opening the file in WordPad will properly display the file. Probably you do not want to be restricted to WordPad. Alternatively you can use code like:
    myfile << "\r\n" to force output. I prefer to use the standard coding: myfile << endl. I wish someone could tell me a way to include the right header, or make the proper settings somewhere to allow me to use the standard coding. In the meantime, you can try the following:
    #define ENDL "\r\n"
    myfile << ENDL;
    The code then looks quite comparable to the standard C++ style, but with capitalization of ENDL throughout your program.
  6. Nov 7, 2012 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The problem arises from
    • The different line endings between Unix and Windows. Cygwin emulates Unix, so it uses Unix line endings.
    • The lack of distinction between binary and text files in Unix. Cygwin by default opens output files in binary mode.
    • C++ I/O, which hides a lot of implementation details, including text versus binary mode.
    • Going back and forth between cygwin and Windows without using dos2unix/unix2dos.
    • Using Notepad which is singularly clueless about Unix line endings.

    There is no problem if you use cygwin tools such as more, cat, and vi to example files produced by applications compiled with g++. There is no problem with regard to notepad and other Windows tools if you use minGW instead of g++. Don't use notepad and you won't have near so many problems. Use dos2unix and unix2dos and you also won't have near so many problems.

    I recommend against using "\r\n". Do this and you'll have problems if you port your code to a Linux machine or to a Windows-aware compiler.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook