2

I have a C app that logs to a file.

I installed the monitor plugin for notepad++ to automatically update, similar to 'tail' on Linux.

My app writes the lines with \n\r at the end ( e.g. fprintf(fp, "%s\n\r","Test"); )

I get something like:

Line 1

Line 2

instead of

Line 1
Line 2

When viewing (no matter the EOL conversion) with 'View->Show Symbol->Show End of Line' I see:

Line 1[CR][LF]
[CR]
Line 2[CR][LF]
[CR]

It is as if it is treating \n as a [CR][LF] and \r as [CR] which is adding another line.

Any ideas?

RobC
  • 502
  • 4
  • 17
  • 2
    does changing the order to `\r\n` change something? – Marco Forberg May 21 '13 at 07:10
  • 1
    Is the problem in notepad++ or in your application? Did you verify if the CR/LF/CR characters are in the file? – Argeman May 21 '13 at 07:11
  • 1
    Try \r\n instead of \n\r – Hikiko May 21 '13 at 07:11
  • \r\n reverses it to [CR] and on the next line [CR][LF] – RobC May 21 '13 at 07:13
  • Then use only \n. Your system/language (C???) seems to insert a CRLF when you use a \n. – Argeman May 21 '13 at 07:15
  • This appears to be a notepad++ issue. It shows up fine in notepad and in editplus. Automatically converting \n into \n\r seems like a mistake. – RobC May 21 '13 at 07:24
  • The code says `fprintf(fp, "%s\n\r","Test")` so both `\n` (ie LF) and `\r` (ie CR) are being written to the file in that order. Notepad++ is not converting anything into `\n\r` as the final `\r` is written by the C program. – AdrianHHH May 21 '13 at 16:08

3 Answers3

2

Try using only \n. The windows sees \n as a new line than \r as a carret return which causes it to break another line, if u use \r\n it would cause only one line break.

Also You can check this article for additional reading.

cerkiewny
  • 2,761
  • 18
  • 36
  • The "In programming languages" section of the linked Wikipedia page, in the paragraph numbered "2", describes what is happening. Basically, in C, the '\n' is converted to the newline sequence used by the system. So in C you almost always just print a "\n" and in some systems it gets converted to a CR-LF pair. – AdrianHHH May 21 '13 at 07:28
  • \r\n merely reverses it to [CR] then [CR][LF] on the next line... notepad++ is converting \n to \n\r automatically as well as treating \r one a line by itself as a newline character when I don't think it should. – RobC May 21 '13 at 07:29
  • I marked this as correct as far as the problem in Notepad++. This is a notepad++ issue, it works fine in every other editor I've tried. I know it isn't the case but as far as I'm concerned \n as newline means that, a new line and no carriage return, and \r is a carriage return, but not necessarily a new line. I am a bit old school in that regard. – RobC May 21 '13 at 07:30
  • No @RobC, Notepad++ is just showing the characters it is given. The C program is adding the extra CR. – AdrianHHH May 21 '13 at 07:31
  • Sorry Adrian I tried this on a linux machine as well, copied the file over, and it still converts it. – RobC May 21 '13 at 07:36
  • @RobC read the Wikipedia article again. On Linux the program will write LF CR into the file (that is not CR LF). Notepad++ is then reading the LF as end-of-line, converting it into CR LF for the Windows line endings. Then Notepad++ reads the CR and keeps it. – AdrianHHH May 21 '13 at 07:46
  • RobC, I know this should work as u said, the \r should be only the carriage return and \n should be only the line break, however some operating systems (not to name them) have assumed that if You are using the carriege return, you need to break the line and standalone \r is concidered a bad thing in them. But thankfully You are able to change the newline character in notepad ++. Try changing the format in settings->preferences->new Document->format. Maybe this will help. – cerkiewny May 21 '13 at 07:51
  • You are correct Adrian, although this happens even with \r\n as well. Perhaps \r isn't really needed much anymore except for strict terminals. It irks me that notepad++ is the only editor that does this though. It really should just treat \n as a newline and ignore \r instead of converting. – RobC May 21 '13 at 07:53
2

I think the problem isn't clear from your description, The title suggests that you have problem with the NPP but your explanation shows that you have problem with the App Code.

for NPP: You can remove those extra new Lines from the file.

for App: If you are strictly going with Windows O.S. then use just '\n' or else if you are using other O.S. also then first find out the new line character for that O.S. and apply that.

Mantra
  • 316
  • 3
  • 16
  • I am trying to view a log file in real time, this is not a good solution for me. It is not an app code problem... \r should mean carriage return, it should not mean a new line as well. – RobC May 21 '13 at 07:31
  • I think you can get some deep insight from following some links: [What is the difference between \r and \n?](http://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n) [Difference between CR LF, LF and CR line break types?](http://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types) [Wiki](https://en.wikipedia.org/wiki/Carriage_return) – Mantra May 21 '13 at 07:42
0

The escape sequences may be os-dependent on the , as I heard some months ago.

So, the \n in this case seems to cover both .

I Would experiment with the escape sequences.

But prior to that : Have a look inside fp.... there could also be some sequences.

icbytes
  • 1,831
  • 1
  • 17
  • 27
  • and inside the file are lines, and they perhaps have already sequences, and You just add Yours to the existing ones printing line after line. Understood ? – icbytes May 21 '13 at 09:17