Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #68635

Re: fprintf with /r/n outputs only one line

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: fprintf with /r/n outputs only one line
Date 2015-08-29 11:22 -0400
Organization Self
Message-ID <55E1CE53.7060704@verizon.net> (permalink)
References <8nnt7k.9dl.19.1@news.alt.net> <mrr94q$t1i$1@dont-email.me> <8no0d3.lp8.17.1@news.alt.net>

Show all headers | View raw


On 08/29/2015 12:02 AM, Dale wrote:
> On 08/28/2015 11:34 PM, James Kuyper wrote:
...
>> On Unix-like systems, newlines are indicated by a single character,
>> '\n'. On other systems, they may be indicated by "\r\n" or "\n\r". On
>> more exotic systems, lines of output may be stored in fixed-sized
>> blocks. The end of the line might be indicated by a counter at the
>> beginning of the block or by having everything past the end of the line
>> padded with blanks (' ') or nulls ('\0'). Other possibilities are also
>> allowed, those are just the more common ones that I'm familiar with.
>>
>> If you open a file in binary mode by adding "b" to the fopen() mode,
>> then you will have to worry about all of those details, and deal with
>> them yourself. However, since you opened your file in text mode, you
>> don't have to worry how the end of the line is represented on disk. When
>> you write "\n" to a file, the appropriate line-ending for the system
>> you're using will be created. When you read the end of a line on input,
>> it will be converted into "\n" by the C standard library's I/O routines.
>>
>> This can cause problems if the system where a file was created uses a
>> different method of indicating the end of the line than that used on the
>> system where the file is read. That's why some people recommend always
>> opening files in binary mode, but in general those people are unwilling
>> to deal with the full range of possible methods for indicating the end
>> of a line. The right solution to this problem is to have a utility
>> routine that can be used to convert a file so that line-endings are
>> marked in the appropriate way for the current system.
>>
> 
> yes, I read about this while trying to google my problem, didn't seem 
> too difficult, think it was a combination of \r and \n
> http://www.thecodingforums.com/threads/carriage-return-and-fprintf.647887/
> 
> the link says unix/linus uses just \n, but \r\n seems to work

Not really: The '\n' works to mark the line, and the '\r' just sits at
the end of the line, where it might or might not be visible, depending
upon how you're viewing the file, and where it might or might not cause
problems, depending upon what you do with the file.

> I don't know how to pull the operating system identification so I can 
> use the appropriate code to end the line and start a new one

There is no standard way to determine which operating system your code
is running on (there's a wide variety non-standard ways, none of which
works universally). That's why the best approach is to use text mode, so
that you don't have to worry about which method is used to mark the end
of the line. Just let the C standard library take care of it for you.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

fprintf with /r/n outputs only one line Dale <dale@dalekelly.org> - 2015-08-28 23:08 -0400
  Re: fprintf with /r/n outputs only one line Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-28 21:25 -0600
    Re: fprintf with /r/n outputs only one line Dale <dale@dalekelly.org> - 2015-08-28 23:56 -0400
  Re: fprintf with /r/n outputs only one line James Kuyper <jameskuyper@verizon.net> - 2015-08-28 23:34 -0400
    Re: fprintf with /r/n outputs only one line Dale <dale@dalekelly.org> - 2015-08-29 00:02 -0400
      Re: fprintf with /r/n outputs only one line Johann Klammer <klammerj@NOSPAM.a1.net> - 2015-08-29 06:51 +0200
        Re: fprintf with /r/n outputs only one line Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-08-29 07:30 +0000
        Re: fprintf with /r/n outputs only one line Keith Thompson <kst-u@mib.org> - 2015-08-29 01:23 -0700
      Re: fprintf with /r/n outputs only one line James Kuyper <jameskuyper@verizon.net> - 2015-08-29 11:22 -0400
        Re: fprintf with /r/n outputs only one line glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-29 16:59 +0000
      Re: fprintf with /r/n outputs only one line Geoff <geoff@invalid.invalid> - 2015-08-30 11:40 -0700
        Re: fprintf with /r/n outputs only one line Keith Thompson <kst-u@mib.org> - 2015-08-30 15:17 -0700
          Re: fprintf with /r/n outputs only one line Geoff <geoff@invalid.invalid> - 2015-08-30 16:21 -0700
            Re: fprintf with /r/n outputs only one line James Kuyper <jameskuyper@verizon.net> - 2015-08-30 19:45 -0400
  Re: fprintf with /r/n outputs only one line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 14:51 +0100
    Re: fprintf with /r/n outputs only one line Ayan <selfjam@gmail.com> - 2015-08-29 10:24 -0400
      Re: fprintf with /r/n outputs only one line James Kuyper <jameskuyper@verizon.net> - 2015-08-29 11:27 -0400
      Re: fprintf with /r/n outputs only one line Ayan <selfjam@gmail.com> - 2015-08-29 14:31 -0400
      Re: fprintf with /r/n outputs only one line Keith Thompson <kst-u@mib.org> - 2015-08-29 13:31 -0700
    Re: fprintf with /r/n outputs only one line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 16:03 +0100
      Re: fprintf with /r/n outputs only one line Ike Naar <ike@iceland.freeshell.org> - 2015-08-29 16:50 +0000
        Re: fprintf with /r/n outputs only one line Ike Naar <ike@iceland.freeshell.org> - 2015-08-29 17:05 +0000
          Re: fprintf with /r/n outputs only one line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-29 19:39 +0100
            Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-29 14:05 -0700
              Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-29 22:46 +0100
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-29 15:59 -0700
                Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-30 00:31 +0100
                Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-30 10:25 +0100
                Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-30 13:22 +0100
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-31 03:18 -0700
                Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-31 11:58 +0100
                Re: fprintf with /r/n outputs only one line Bartc <bc@freeuk.com> - 2015-08-31 13:38 +0100
                Re: fprintf with /r/n outputs only one line glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-09-04 04:18 +0000
                Re: fprintf with /r/n outputs only one line "Charles Richmond" <numerist@aquaporin4.com> - 2015-09-04 05:09 -0500
                Re: fprintf with /r/n outputs only one line Robert Wessel <robertwessel2@yahoo.com> - 2015-09-04 05:19 -0500
                Re: fprintf with /r/n outputs only one line raltbos@xs4all.nl (Richard Bos) - 2015-09-04 16:33 +0000
              Re: fprintf with /r/n outputs only one line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 02:17 +0100
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-29 23:43 -0700
                Re: fprintf with /r/n outputs only one line Geoff <geoff@invalid.invalid> - 2015-08-30 11:47 -0700
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-30 13:18 -0700
                Re: fprintf with /r/n outputs only one line Geoff <geoff@invalid.invalid> - 2015-08-30 16:04 -0700
              Re: fprintf with /r/n outputs only one line Ike Naar <ike@iceland.freeshell.org> - 2015-08-30 07:20 +0000
              Re: fprintf with /r/n outputs only one line Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 15:17 -0700
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-06 16:36 -0700
                Re: fprintf with /r/n outputs only one line Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-07 23:19 -0700
                Re: fprintf with /r/n outputs only one line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 02:50 -0700
            Re: fprintf with /r/n outputs only one line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-30 02:14 +0100

csiph-web