'\n' interpreted different ways Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

'\n' interpreted different ways

Post by MoonKid » Thu Jul 10, 2008 3:51 pm

I write a log file with a wx application and end each log with a '\n'.

But it is interpreted different by notepad.exe and notepad++ as you can see in this screenshot.

Image

It looks like that notebook.exe ignore my new-lines.

That is the simple code

Code: Select all

// ...
fileLog_(WXC_LOG, wxFile::write_append)
// ...
/*static*/ void WXCLog::Do(const wxString& str, bool bWithTimestamp /*= true*/)
{
    wxString strLog;

    // ...
        strLog << str << '\n';

    // write to the file
    Instance().fileLog_.Write(strLog);

    // ...
}
I am using wx2.9 from SVN with WinXPSP2.

User avatar
doublemax
Moderator
Moderator
Posts: 15273
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Thu Jul 10, 2008 4:04 pm

\n is a unix linefeed. For Windows you probably need \r\n
Use the source, Luke!

MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

Post by MoonKid » Thu Jul 10, 2008 4:49 pm

But isn't there a portable wx-way to set a newline?

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Thu Jul 10, 2008 5:18 pm

MoonKid wrote:But isn't there a portable wx-way to set a newline?
No... That is beyond wx's control. notepad, notepad++ or any other editor is responsible for how they interpret and display a file. Some (mostly on windows) use \r\n to mean a new line, others (unix) use just \n. notepad++ I am not familiar with but the name makes me think it is for programmers and thus is probably smart enough to handle either.

But to sum up, wx can't control how another program chooses to interpret data. Wx will provide you a portable way to write that data to a file, but what some other program then does with the data is up to that program.

-Max

MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

Post by MoonKid » Thu Jul 10, 2008 6:13 pm

We know that unix use '\n' and windows use '\r\n'. It doesn't matter how other applications interpret this.

There is a portable way to specify pat-separators ('\' and '/' with wxPATH_SEPARATOR).

Something like this should exist for newlines, too.

JudyL
Experienced Solver
Experienced Solver
Posts: 66
Joined: Tue Jul 24, 2007 2:20 pm
Location: Florida, USA

Post by JudyL » Thu Jul 10, 2008 7:30 pm

MoonKid wrote:We know that unix use '\n' and windows use '\r\n'. It doesn't matter how other applications interpret this.
Not true. That distinction is not dictated by the OS. It is determined by the programs that read and write text files. There is no hard and fast rule on an operating system as to what constitutes a "newline". It depends on the program, not the OS. I've got files that end in \r\n that open fine on Linux and files that end in \n that open file in Windows. There is "what the majority does" but that is not a must like / versus \.

Judy

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Jul 10, 2008 7:45 pm

Easy enoguh :

Code: Select all

#ifdef __WXMSW__
#define LINE_BREAK "\r\n"
#else
#define LINE_BREAK "\n"
#endif

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Thu Jul 10, 2008 11:11 pm

Just use the standard C++ streams instead of wxFile.

They take care of the linefeends, no need to do it yourself. Write "\n", the standardlib will convert it automatically to "\r\n" on windows.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Fri Jul 11, 2008 4:13 am

Frank wrote:Just use the standard C++ streams instead of wxFile.

They take care of the linefeends, no need to do it yourself. Write "\n", the standardlib will convert it automatically to "\r\n" on windows.
Not sure that this fix's the OP original problem only changes it. Some small number of programs on windows(those expecting \n only) will show an extra line feed.

Furthermore, if the OP's program outputs \n on some systems and \r\n on others opening 2 files on created on windows and on OSX side by side will show two different results. Not sure if this is desireable. Most users expect that if an app is available on multiple platforms that the file created by that app should be consistant across all those platforms.

Imagine OpenOffice or MS Office creating different word files on unix and windows?

Just my 2 cents.

-Max

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Fri Jul 11, 2008 8:12 am

EVERY windows program can interpret \r\n, because almost every windows program writes \r\n.

The only thing, some windows programs not recognise is \n because, well, almost every windows program writes \r\n.

Post Reply