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


Groups > comp.lang.c > #99749

Re: Line read from csv file with fgets has two characters before the terminating one

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Line read from csv file with fgets has two characters before the terminating one
Date 2016-12-17 15:33 +0000
Organization A noiseless patient Spider
Message-ID <87mvfuy3hc.fsf@bsb.me.uk> (permalink)
References (10 earlier) <o30isq$81f$1@dont-email.me> <87shpn25ea.fsf@bsb.me.uk> <o31oie$gfm$1@dont-email.me> <87a8bvz2x3.fsf@bsb.me.uk> <o338v7$dil$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:

> On 17/12/2016 02:48, Ben Bacarisse wrote:
>> BartC <bc@freeuk.com> writes:
>
>>> Testing for EOF would make sense if the user inadvertently redirected
>>> console input to come from a file.
>>
>> Or inadvertently closed the stdin stream.
>
> Or inadvertently paused it with Ctrl S. Or inadvertently pressed Ctrl
> C and they have to start again. In this application little can
> seriously go wrong.

No, I gave another example of why the test you won't add would be
useful.  These are example of other things which you might want to
consider as well, but they are much more complicated to deal with.

What I can't understand is the dogged Resistance to adding a simple test
that clearly reduces the change of a user surprise.

>>> Then if there are more blocks in
>>> the dumped file than there are newline characters in the file,
>>> eventually the latter will only return EOF and no more Enters.
>>
>> Yes, and without an EOF check the program will not end.  It's not a
>> common problem but the fix is so trivial it seems churlish to keep
>> arguing against using it.
>
> I spent much of my career writing interactive graphics
> applications. I'm not going to go back to 1970s style Unix utilities.

What?  This is not an interactive graphics application, nor is it a Unix
style utility (from the 70s or any other decade).  You could make it
into either if you wished, but I was offering a suggestion about the
code you actually wrote.

If you post code, people will comment on it.  If you don't like a
particular comment, just say something like "thanks, but this program
does what I want and I don't want to make any changes to it".

>>> But this would be getting a long way from the kind of pause()
>>> functions I was using last century which waited for the next key press
>>> or mouse click. It would be going backwards.
>>
>> There's not need to go back -- you can keep using if you like.  But if
>> you are going to do the pause the way you wrote it, it's better to
>> include an EOF test.
>>
>> But the modern way is to write utilities that combine well together.  I
>> want to chose how to page through a program's output.  I don't wany you
>> do decide what facilities I get for that.  You don't even try to get the
>> page size right for my terminal.
>
> Have you seen the code needed to get the size of a terminal?

Yes, of course.  That's why I'm not suggesting you do that.

> Which is
> also different across OSes unless you then drag in cross-platform
> third party libraries. You forget this is a compact, self-contained
> program posted as a c.l.c example. And the bigger it is, the more
> things it will have that people won't like.

Exactly.  To my mind, it has just one feature too many -- guessing what
page size the user would like and prompting to display the next page.
I'm sure some people will like the choices you've made, but I prefer a
more flexible utility.

> Anyway it's also important the block size is significant. and I chose
> a power of two: 512 bytes (also a typical sector size), displayed as
> 32 rows of 16 bytes. I supposed that could be reduced to 16 rows to
> accommodate screens of 24 rows.

I have no objection to marking the blocks in some useful way, but
there's no reason that has to be linked to a display size.

>>> You just take out the lines that wait for a response.
>
>> (I'm joking, it's considered
>> bonkers!)
>
> Why is it bonkers? It's presented in a few dozen lines of C source in
> a thread about learning C.

No.  A reader could have taken your posting as being useful hex dump
utility that could already be used to collect the output in a file (for
example so the student can post it here).

We're in agreement.  The program is not so good for that as it stands.

>> I'd edit the source to (a) not prompt, (b) not wait, and (c) not insist
>> on a file name.
>
> So someone is supposed to run it as:
>
>    dump <file
>
> Then anyone who types 'dump' or 'dump file' will see one of those
> mysterious programs that appears to hang.

No, I only said "not insist on a file name".  I would want a file name
to be accepted.  It will, however, just sit there if you type

  dump

(With some programs that insist on a file I can get round it by using
/dev/stdin, but that obviously won't work with a program like yours that
uses stdin already.)

<snip>
> Apart from which, if they do 'dump <file' on a Windows TXT file, it
> will likely suppress the CR part of CRLF line endings.

A dump utility that can read stdin should try to read it in binary mode
but I'm pretty not sure freopen with a NULL file name does not work in
Windows.  So, yes, writing a portable version of the sort of utility
that is common in the Unix word is almost impossible.  Insisting on a
file name gets round that.

Editing the code was your suggestion for customising it, so I was saying
what I'd want.

<snip>
> I can imagine that if you posted such a program here, if wouldn't be a
> dump program at all, or not one of any use, unless it's invoked under
> Unix in a certain way (and presumably with a bunch of options) and in
> conjunction with other utilities.

I'm not sure what the "other utilities" remark is about, but you are
right that I would have posted "Unixy" code without thinking of the
portability.

> In that case there wouldn't be any point as you would just run
> hexdump.

Right.  And I hope that, having posted my Unixy version, you would point
out the trouble with dumping stdin and I would say say "Good point".

One solution (for a program aimed at students) would be to print a
warning (to stderr) that the program is reading from a text stream and
that only "dump file" is reliable.

-- 
Ben.

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


Thread

Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-14 00:04 -0800
  Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 11:10 +0000
    Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-14 23:50 -0800
      Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:52 +0000
        Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 00:58 -0800
          Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 09:09 +0000
            Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 04:23 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:50 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 14:14 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:42 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 09:49 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-15 17:58 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 18:22 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 11:51 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 20:35 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 12:58 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-15 13:11 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 02:14 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-16 11:25 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 16:41 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-16 22:08 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-17 02:48 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 11:54 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-17 15:33 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-17 08:27 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 17:25 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-17 09:53 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 18:21 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-17 10:30 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 22:39 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-17 14:51 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 23:12 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-18 08:37 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-18 13:14 -0800
            Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-15 15:03 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 15:29 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-15 20:48 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 22:58 -0800
                OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 10:22 +0000
                Re: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one) Geoff <geoff@invalid.invalid> - 2016-12-16 07:29 -0800
                Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-17 10:06 +0000
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) Geoff <geoff@invalid.invalid> - 2016-12-17 12:39 -0800
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-18 13:54 +0000
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) Geoff <geoff@invalid.invalid> - 2016-12-18 17:57 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-16 07:12 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-16 12:02 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 23:29 +0000
              Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-16 02:37 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-16 07:51 -0800
          Re: Line read from csv file with fgets has two characters before the terminating one Manfred <noname@invalid.add> - 2016-12-15 12:22 +0100
            Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 11:29 +0000
              Re: Line read from csv file with fgets has two characters before the terminating one Melzzzzz <Melzzzzz@zzzzz.com> - 2016-12-15 12:40 +0100
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:00 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Melzzzzz <mel@zzzzz.com> - 2016-12-15 13:09 +0100
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:43 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-15 09:18 -0500
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:49 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-15 10:34 -0500
              Re: Line read from csv file with fgets has two characters before the terminating one Manfred <noname@invalid.add> - 2016-12-15 16:34 +0100
    Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 00:22 -0800
      Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 09:04 +0000
  Re: Line read from csv file with fgets has two characters before the terminating one mark.bluemel@gmail.com - 2016-12-14 03:38 -0800

csiph-web