• Support PF! Buy your school textbooks, materials and every day products Here!

Endl and /n not working in g++?

  • Thread starter crazyshark
  • Start date
  • #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.
 

Answers and Replies

  • #2
33,514
5,195
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.
 
  • #3
I like Serena
Homework Helper
6,577
176
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".
 
  • #4
1
0
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.
 
  • #5
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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.
 

Related Threads on Endl and /n not working in g++?

Replies
24
Views
4K
Replies
1
Views
5K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
1
Views
3K
Replies
3
Views
6K
Replies
2
Views
6K
  • Last Post
Replies
8
Views
4K
  • Last Post
Replies
3
Views
748
Top