Endl and /n not working in g++?

  • Thread starter Thread starter crazyshark
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around issues related to output formatting in C++ programs compiled with g++ on Cygwin, particularly concerning the handling of newline characters and line endings when writing to text files. Participants explore differences in behavior between Visual Studio and g++, as well as the implications of using different operating systems.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that their program works in Visual Studio but fails to produce the expected output in g++, suggesting a potential issue with newline characters.
  • Another participant proposes that the difference in line endings between Windows and Unix/Linux could be the cause, highlighting that Windows uses a combination of carriage return and line feed, while Unix/Linux uses just one character.
  • A suggestion is made to inspect the output file with different text editors, indicating that WordPad may display the content correctly compared to Notepad.
  • Some participants recommend using "\r\n" to force the correct line ending, while others prefer to use the standard C++ endl, expressing a desire for a solution that allows the use of standard coding practices.
  • Concerns are raised about the implications of using different modes for opening files in Cygwin, particularly regarding binary versus text modes and the potential issues when switching between Cygwin and Windows environments.
  • One participant advises against using "\r\n" due to potential portability issues when moving code to different systems.

Areas of Agreement / Disagreement

Participants generally agree that the differences in line endings between Unix and Windows are a significant factor in the issues experienced. However, there are competing views on the best approach to resolve the problem, with some advocating for specific coding practices while others caution against them.

Contextual Notes

Limitations include the lack of consensus on the best method for handling line endings in C++ code across different environments, as well as the potential confusion arising from the use of different text editors and file modes.

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
4K
  • · 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
3K
Replies
17
Views
2K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K