"\n" vs. '\n'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Juha Nieminen

    "\n" vs. '\n'

    I assume that using '\n' to print a newline is not portable because in
    some systems newline consists actually of two characters and '\n' is
    only one. Thus the only portable way of printing a newline character
    (for example with std::printf) is "\n".

    What does the standard say about '\n'? Can any assumptions be made
    about it? Or is it a "never use it in portable code" value?

    Of course this might present a problem when reading an input file in a
    byte-by-byte fashion. If you want to detect a newline while reading it
    like that, can you simply compare a read byte with '\n'? What if a
    newline actually consists of more than one character?

    The std::getline() function accepts a delimiter character as third
    parameter (the input is read until this character appears). Since the
    type of this parameter is char, doesn't this actually present a problem
    if you want to give it a newline as delimiter? (I know that newline is
    the default delimiter, but in theory it might be possible that you might
    want to be able to specify it explicitly...)
    If you repeatedly call std::getline(is , str, '\n') in a system where
    newlines actually consist of more than one character, aren't the
    additional characters read into the string (even though they really
    shouldn't) instead of skipped? There doesn't seem to be a version of
    std::getline() taking a string as delimiter parameter, so you can't give
    it a "\n".
  • peter koch

    #2
    Re: "\n&quo t; vs. '\n'

    On 8 Aug., 12:28, Juha Nieminen <nos...@thanks. invalidwrote:
    I assume that using '\n' to print a newline is not portable because in
    some systems newline consists actually of two characters and '\n' is
    only one. Thus the only portable way of printing a newline character
    (for example with std::printf) is "\n".
    >
    What does the standard say about '\n'? Can any assumptions be made
    about it? Or is it a "never use it in portable code" value?
    >
    Of course this might present a problem when reading an input file in a
    byte-by-byte fashion. If you want to detect a newline while reading it
    like that, can you simply compare a read byte with '\n'? What if a
    newline actually consists of more than one character?
    >
    The std::getline() function accepts a delimiter character as third
    parameter (the input is read until this character appears). Since the
    type of this parameter is char, doesn't this actually present a problem
    if you want to give it a newline as delimiter? (I know that newline is
    the default delimiter, but in theory it might be possible that you might
    want to be able to specify it explicitly...)
    If you repeatedly call std::getline(is , str, '\n') in a system where
    newlines actually consist of more than one character, aren't the
    additional characters read into the string (even though they really
    shouldn't) instead of skipped? There doesn't seem to be a version of
    std::getline() taking a string as delimiter parameter, so you can't give
    it a "\n".
    For the user endline is \n and always one character. An operating
    systems may encode endline as one character, two characters or no
    character at all (I do not know of any other way to do it, but there
    could possibly be more). It is the job of the io machinery to
    translate these representations into a single endline character unless
    you choose to open the stream/file in binary mode.

    /Peter

    Comment

    • Pete Becker

      #3
      Re: &quot;\n&quo t; vs. '\n'

      On 2007-08-08 06:28:47 -0400, Juha Nieminen <nospam@thanks. invalidsaid:
      I assume that using '\n' to print a newline is not portable because in
      some systems newline consists actually of two characters and '\n' is
      only one. Thus the only portable way of printing a newline character
      (for example with std::printf) is "\n".
      '\n' is a character. "\n" is a string consisting of two characters:
      '\n' and '\0'.
      >
      What does the standard say about '\n'? Can any assumptions be made
      about it? Or is it a "never use it in portable code" value?
      '\n' is a newline character. When it's written to a text stream the
      runtime library will do whatever is appropriate for the system the
      program was compiled for. These both have the same effect:

      printf("\n");
      printf("%c", '\n');
      >
      Of course this might present a problem when reading an input file in a
      byte-by-byte fashion. If you want to detect a newline while reading it
      like that, can you simply compare a read byte with '\n'? What if a
      newline actually consists of more than one character?
      If you're reading in text mode, the system's end-of-line representation
      will be translated to the character '\n'. If you're reading in binary
      mode you get whatever bytes are in the file.
      >
      The std::getline() function accepts a delimiter character as third
      parameter (the input is read until this character appears). Since the
      type of this parameter is char, doesn't this actually present a problem
      if you want to give it a newline as delimiter? (I know that newline is
      the default delimiter, but in theory it might be possible that you might
      want to be able to specify it explicitly...)
      If you're calling getline() you'd better be using a text stream. The
      system's end-of-line representation will be translated to the character
      '\n'.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      Working...