Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] C++ Problem - about endl & \n

Status
Not open for further replies.

guang4243

Newbie level 5
Newbie level 5
Joined
Sep 27, 2011
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Malaysia
Activity points
1,338
Hello, i now learning to write C++ code in linux platform
but some how i face some problem:
Here is my coding:

#include<iostream>
#include<fstream>
using namespce std;
int main()
{
ofstream Hello_test;
Hello_test.open("Hello.txt");
Hello_test<<"Test...\nTest..."<<endl;
Hello_test<<"Hello World"<<endl;
Hello_test.close();
}


After that, i success compile and run it.
In the Hello.txt file, it show that:

Test...
Test...
Hello World

This is good, because i get what i need. But when i copy this Hello.txt file to window platform, it show that:

Test...Test...Hello World

Here is my question, why the text file does not show the next line where every thing already combine together.
Is there anywhere can solve this problem?
Please Help
 

Hello there!

I had same problem, too a few time ago. After searching and doing some tests, I realized the following: Linux handles newline character '\n' as it should (i.e., it forces a newline), but Windows uses a '\n\r' newline+carriage return pair (don't know about the order, though) to force newline. However, if you use Wordpad to open your .txt file, it should handle '\n' correctly. That is, it is NOT a programming bug or something related to the compiler, but it's Windows that handles the characters differently.

Cheers
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top