2

in windows, a line break is

\r\n

in linux,

\n

I don't want to change my code when it runs in different OS, I just want to recompile it directly.

So how to detect it in C++? Did the standard library defined a constant or function to detect it?

Tio Plato
  • 132
  • 1
  • 9

3 Answers3

1

You shouldn't have to change your code. Each implementation will correctly handle it's own line endings.

Provided each platform read and writes its own files, it will work fine.

The only problem you'll have is if you transfer a Windows-style data file to UNIX or vice versa. If that's the sort of thing you're doing, you can simply modify the string that you read in so that any \r character at the end is removed.

Note that won't adversely affect Windows-style files since, in memory, they don't have the \r - it's only put in the file when writing.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Uhm it depends if you read the content on binary mode or text mode, additionally network protocols like http/smtp use \r\n to specify the new line. If he uses only text mode, you are completely right, if he mixes binary/text mode, he should be really careful with this aspect, and as I said, if he works with network protocols, he should consider them. – Jose Palma Apr 09 '15 at 06:55
  • I don't think you need to bother about reading UNIX files on Windows, - the C library usually ignores the missing `\r`. – MSalters Apr 09 '15 at 07:00
1

If you don't move your files between Windows/Linux, you should be fine.

However, in many circumstances, you have to read both conventions from both compiled programs. This is common with all sorts of documents and some network protocols. If this is the case, I recommend the following procedure:

  1. Scan for \r or \n.
  2. If char found is \r, then also look for \n immediately after it. If it exists, then consume it as part of the same newline.

This scanning procedure will accept files with CR, LF, and CR+LF line endings. It is analogous to what many encoding-detecting higher level string streams do.

Here is another answer which outlines this procedure in code for istream: Getting std :: ifstream to handle LF, CR, and CRLF?

Community
  • 1
  • 1
VoidStar
  • 5,241
  • 1
  • 31
  • 45
  • Is there any other line break symbol ? – Tio Plato Apr 09 '15 at 07:32
  • More exist, see http://en.wikipedia.org/wiki/Newline . But IMO they are not worth supportong. `CR`, `LF`, and `CR+LF` are the big 3 for general multiplatform support. – VoidStar Apr 09 '15 at 08:23
1

Platform dependent details you need to encapsulate in abstraction. So just provide simple class TextInfo (?) and add there static function or maybe public static member which will return the line ending.

In this function you can add platform specific code with conditional compilation using preprocesor defines.

#ifdef WINDOWS
return "\r\n";
#endif
#ifdef LINUX
return "\n";
#endif

Now you just need to provide different target compilations (probably you already have that). For targets you need to add one more define to compile command (-D for gcc and /D for VS).

Then in code you are able to use TextInfo which will work fine for both systems. At any time you are able to add additional systems.

senfen
  • 877
  • 5
  • 21
  • 5
    You are wrong. #ifdef LINUX doesn't work at all, you should replace it with #ifdef unix, And #ifdef WINDOWS also is wrong, replace it with #ifdef WIN32 – Tio Plato Apr 09 '15 at 07:29
  • Pleas read the whole answer. Also with this part: `For targets you need to add one more define to compile command.` I don't use 'standard' defines because there are compilator dependent. Provide you own by adding them to compile command. – senfen Apr 09 '15 at 07:36
  • I got your idea. But please use the standard. – Tio Plato Apr 09 '15 at 07:41
  • And I want to know, whether the standard library defined similar class? – Tio Plato Apr 09 '15 at 07:42
  • 2
    But there are no standard defines like these! And that's why I put it in quotes. In different compiler you will need to use different macro to check that so best way is to provide own. Both macros which you shown are compiler-specific macro. You like to have generality in newline using compiler-specific macros? – senfen Apr 09 '15 at 07:46
  • There are no standard class for that and there is no standard class for difference in '\' and '/' on unix and windows directories :) – senfen Apr 09 '15 at 07:51