Endl and /n not working in g++?

  • Thread starter Thread starter crazyshark
  • Start date Start date
Click For Summary
The issue with using g++ in Cygwin stems from the differences in line endings between Windows and Unix/Linux systems, with Windows requiring both carriage return and line feed (CRLF) while Unix uses just line feed (LF). When compiling in Cygwin, the default setting uses Unix-style line endings, which can cause output to appear on a single line in Windows applications like Notepad. To resolve this, users can explicitly include "\r\n" in their output or define a custom ENDL macro to maintain compatibility with standard C++ practices. Additionally, using tools like dos2unix and unix2dos can help manage line endings when transferring files between systems. Overall, avoiding Notepad for viewing files and using appropriate Cygwin tools can mitigate these issues.
crazyshark
Messages
1
Reaction score
0
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.
 
Physics news on Phys.org
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.
 
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".
 
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.
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
17
Views
2K
  • · Replies 28 ·
Replies
28
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K