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


Groups > comp.lang.c > #126750

Re: Text Files

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Text Files
Date 2018-02-15 20:32 +0000
Organization A noiseless patient Spider
Message-ID <87a7wa8040.fsf@bsb.me.uk> (permalink)
References <5a8459d1$0$707$14726298@news.sunsite.dk> <lifhC.9897$lJ4.4815@fx27.am4> <87o9kq8gh3.fsf@bsb.me.uk> <oUhhC.249883$w26.130771@fx47.am4>

Show all headers | View raw


bartc <bc@freeuk.com> writes:

> On 15/02/2018 14:39, Ben Bacarisse wrote:
>> bartc <bc@freeuk.com> writes:
>
>>> Here's my C coding exercise for the morning. It uses a library-based
>>> approach: put all the fiddly details into functions, in the aim of
>>> keeping the main function as simple and as close to the above as
>>> possible.
>
>> But more importantly the OP probably wants to see the C way of doing it
>> rather trying to shoe-horn C into what is probably just an illustrative
>> example.
>
> I don't think the OP is enough of a beginner not to know how to apply
> an array to this example.

That comment was about the forced IO style, not about using an array.

>>   I can't shake the feeling you are
>> doing this to further your agenda of making C look more complicated than
>> it is.
>
> Perhaps people can compare my code with yours, and they can decide for
> themselves which is more complicated.

That's why I posted it.  As I said, I'm sure there are plenty of people
who won't like it.  Maybe we can get a version from some of them.  It's
always good to see lots of versions.

>> Why use int64_t?
>
> That's the maximum easily usable integer size in C.

No it's not.  There is such a type (intmax_t) but even using that would
miss the point.

> The main program only uses 32 bit ints;

No, it uses ints.

> yes the library elements are
> more general than this one specific application of them.
>
>> If you wanted a more
>> general readint function then long long is the type to use since it is
>> guaranteed to be available, is at least 64 bits wide, and matches the
>> type of strtoll.
>
> That's an interesting remark coming from you, as your code fragments
> generally make use of complicated features such as VLAs, variable
> types (those multi-dimensional types in parameters), designated
> initialisers, wide strings, compound literals and generics. [None of
> which my C compiler supports!]

Eh?  I am not against using any particular feature of the language.
When you need a 64-bit 2's complement signed int, int64_t is the way to
go.  But here I just can't see why you want to over-specify the type
when you can't do any better than use long long -- if long long int is
wider than 64-bits you'll get into trouble with some conversions, and if
it's narrower than 64-bits you won't be able to actually read a 64-bit
as the function appears to promise (from the type).

> But int64_t is a simple typedef if not available.

If int64_t is not available on a conforming implementation it can't be
made available by a typedef.

>> Why do your signature EOF dance when you should know by now that C
>> favours a "try it and see if worked" model of IO?
>
> You mean C's 'keep walking until you've fallen off the edge of the
> cliff' idiom, rather than my 'if not at the edge of the cliff, take
> another step'?

No.  I meant what I said.  C's style of IO does not involve any peril as
your humorous re-write suggests.  It's simply a better way to do input. 

>> #include <stdio.h>
>> #include <stdbool.h>
>>
>> bool parse_line(const char *lp, size_t n_wanted, int *d)
>> {
>>       int n, len;
>>       size_t n_read = 0;
>>       while (sscanf(lp, "%d %n", &n, &len) == 1) {
>>            if (n_read < n_wanted)
>>                 d[n_read] = n;
>>            n_read += 1;
>>            lp += len + (lp[len] == ',' || lp[len] == ';');
>>       }
>>       return *lp == 0 && n_read == n_wanted;
>> }
>>
>> int main(void)
>> {
>>       char line[1000];
>>       size_t lnum = 0, numbers_per_line = 6;
>>       int data[numbers_per_line];
>>
>>       while (fgets(line, sizeof line, stdin)) {
>>            lnum += 1;
>>            if (parse_line(line, numbers_per_line, data)) {
>>                 for (size_t i = 0; i < numbers_per_line; i++)
>>                      printf(" %5d", data[i]);
>>                 putchar('\n');
>>            }
>>            else fprintf(stderr, "Bad input on line %zu\n", lnum);
>>       }
>> }
>
> I thought at last, I can run one of Ben's examples with my
> compiler. But it says, 'Can't do VLAs'! I needed to change
> numbers_per_line to an enum.

If a post looks like probable coursework I try to put "talking points"
into it.  If a student copies it, the instructor can ask things like
"show me where you use a VLA", "explain the space in the sscanf
format" and "what does %zu mean in that printf?".

> Note that on error, your program says 'Bad input on line zu'.

Not for me.  Something is probably wrong at your end.

> And you have a go at me for using 'int64_t'!

Yes, for the reasons I explained which are not because it's "new", or
"fancy" but because it's not the best type to use where you used it.
You'd have a point if I'd complained about using some new-fang led thing
like VLAs (which have not been around for two decades yet), but I didn't
do that.

-- 
Ben.

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


Thread

Text Files G B <gb@gb.com> - 2018-02-14 11:47 -0400
  Re: Text Files Ed Prochak <edprochak@gmail.com> - 2018-02-14 08:45 -0800
  Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-14 08:47 -0800
    Re: Text Files already5chosen@yahoo.com - 2018-02-15 01:17 -0800
  Re: Text Files gordonb.ea2mq@burditt.org (Gordon Burditt) - 2018-02-14 11:10 -0600
  Re: Text Files "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-14 12:28 -0500
  Re: Text Files jt@toerring.de (Jens Thoms Toerring) - 2018-02-14 21:07 +0000
  Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-14 15:31 -0700
    Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-15 06:59 +0000
      Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 10:20 +0000
  Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-14 22:51 +0000
    Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-15 11:04 +0100
      Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-15 06:16 -0800
        Re: Text Files "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-02-15 22:22 +0800
        Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-15 15:58 +0100
          Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 16:31 +0000
            Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-15 21:31 +0100
              Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 21:21 +0000
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 22:17 +0000
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 23:36 +0000
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 23:48 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 10:09 +0100
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 12:57 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 15:50 +0100
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 15:50 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 17:07 +0100
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 20:44 +0000
                Re: Text Files Ian Collins <ian-news@hotmail.com> - 2018-02-17 11:22 +1300
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 17:48 +0000
                Re: Text Files GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-16 17:06 +0100
                Re: Text Files Ian Collins <ian-news@hotmail.com> - 2018-02-17 11:10 +1300
                Re: Text Files supercat@casperkitty.com - 2018-02-16 14:48 -0800
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-16 15:14 -0800
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 23:25 +0000
                Re: Text Files supercat@casperkitty.com - 2018-02-16 15:50 -0800
          Re: Text Files already5chosen@yahoo.com - 2018-02-16 07:19 -0800
            Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 17:13 +0100
              Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-19 17:55 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-19 19:14 +0100
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-19 11:48 -0800
            Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-16 08:14 -0800
              Re: Text Files Robert Wessel <robertwessel2@yahoo.com> - 2018-02-17 18:32 -0600
    Re: Text Files jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-15 22:57 +0100
      Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 22:58 +0000
        Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 00:19 +0000
          Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 02:26 +0000
            Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 02:57 +0000
              Re: Text Files asetofsymbols@gmail.com - 2018-02-16 00:21 -0800
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 11:30 +0000
          Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 10:36 +0100
            Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 11:36 +0000
              Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 13:10 +0100
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 12:25 +0000
                Re: Text Files Ike Naar <ike@iceland.freeshell.org> - 2018-03-06 19:33 +0000
                Re: Text Files bartc <bc@freeuk.com> - 2018-03-06 20:55 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-16 04:42 -0800
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 16:58 +0100
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 20:02 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 21:43 +0100
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-17 04:01 -0800
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-17 13:10 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-17 05:27 -0800
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-17 14:20 +0000
                Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-16 09:04 -0700
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 14:31 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-16 06:42 -0800
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-16 14:58 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-16 07:23 -0800
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-16 15:24 +0000
              Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-16 04:13 -0800
                Re: Text Files Siri Cruise <chine.bleu@yahoo.com> - 2018-02-16 04:29 -0800
                Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-20 16:41 +0000
                Re: Text Files Siri Cruise <chine.bleu@yahoo.com> - 2018-02-20 09:39 -0800
                Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-20 19:16 +0000
  Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 12:46 +0000
    Re: Text Files GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-15 14:52 +0100
    Re: Text Files Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-15 17:07 +0300
      Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 14:23 +0000
        Re: Text Files Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-15 18:52 +0300
    Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 14:39 +0000
      Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 15:43 +0000
        Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 20:32 +0000
          Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 22:06 +0000
            Re: Text Files GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-15 23:12 +0100
            Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-15 15:12 -0800
              Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 23:43 +0000
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 23:54 +0000
            Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 23:35 +0000
          Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-16 17:07 -0800
            Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-17 02:38 +0000
              Re: Text Files Ian Collins <ian-news@hotmail.com> - 2018-02-17 17:26 +1300
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-17 11:22 +0000
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-17 12:07 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-17 04:17 -0800
                Re: Text Files Anton Shepelev <anton.txt@gmail.com> - 2018-02-17 16:25 +0300
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-17 07:35 -0800
                Re: Text Files GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-02-17 15:24 +0100
                Re: Text Files Ian Collins <ian-news@hotmail.com> - 2018-02-18 11:10 +1300
              Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 12:05 -0800
                Re: Text Files already5chosen@yahoo.com - 2018-02-19 15:32 -0800
                Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 10:34 -0800
                Re: Text Files already5chosen@yahoo.com - 2018-02-21 15:35 -0800
                Re: Text Files bartc <bc@freeuk.com> - 2018-02-22 00:11 +0000
                Re: Text Files Robert Wessel <robertwessel2@yahoo.com> - 2018-02-21 18:26 -0600
                Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 21:39 -0800
                Re: Text Files Robert Wessel <robertwessel2@yahoo.com> - 2018-02-23 03:08 -0600
                Re: Text Files supercat@casperkitty.com - 2018-02-23 16:16 -0800
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-23 16:46 -0800
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-24 02:49 -0800
                Re: Text Files asetofsymbols@gmail.com - 2018-02-24 05:16 -0800
                Re: Text Files "F. Russell" <fr@random.info> - 2018-02-24 14:35 +0000
                Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-24 08:48 -0700
                Re: Text Files supercat@casperkitty.com - 2018-02-25 15:09 -0800
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-25 15:54 -0800
                Re: Text Files supercat@casperkitty.com - 2018-02-26 11:04 -0800
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-26 11:52 -0800
                Re: Text Files supercat@casperkitty.com - 2018-02-26 14:12 -0800
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-26 14:40 -0800
                Re: Text Files already5chosen@yahoo.com - 2018-02-22 04:58 -0800
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-22 05:33 -0800
                Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-22 13:59 +0000
                Re: Text Files Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-22 07:24 -0800
              Re: Text Files supercat@casperkitty.com - 2018-02-20 12:14 -0800
        Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-15 21:37 +0100
          Re: Text Files bartc <bc@freeuk.com> - 2018-02-15 21:34 +0000
            Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-15 14:15 -0800
            Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-16 10:47 +0100
  Re: Text Files "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-02-15 22:20 +0800
  Re: Text Files "F. Russell" <fr@random.info> - 2018-02-15 15:24 +0000
    Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-15 16:05 +0000
    Re: Text Files "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2018-02-16 00:22 +0800
    Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-15 13:35 -0700
  Re: Text Files Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 00:13 +0300
    Re: Text Files bartc <bc@freeuk.com> - 2018-02-24 21:24 +0000
      Re: Text Files Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 14:55 +0300
        Re: Text Files bartc <bc@freeuk.com> - 2018-02-25 13:00 +0000
    Re: Text Files Real Troll <Real.Troll@Trolls.com> - 2018-02-24 17:35 -0400
    Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-24 21:36 +0000
      Re: Text Files Real Troll <Real.Troll@Trolls.com> - 2018-02-24 17:45 -0400
      Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-24 22:51 +0000
        Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-24 23:56 +0000
          Re: Text Files bartc <bc@freeuk.com> - 2018-02-25 00:14 +0000
            Re: Text Files bartc <bc@freeuk.com> - 2018-02-25 00:22 +0000
            Re: Text Files Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 15:16 +0300
              Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-25 15:20 -0800
                Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-25 20:27 -0700
                Re: Text Files gazelle@shell.xmission.com (Kenny McCormack) - 2018-02-26 06:19 +0000
                Re: Text Files Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-26 15:35 +0300
                Re: Text Files Reinhardt Behm <rbehm@hushmail.com> - 2018-02-26 20:53 +0800
                Grammar Patrol (Was: Text Files) gazelle@shell.xmission.com (Kenny McCormack) - 2018-02-26 17:54 +0000
                Re: Text Files Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-26 15:27 +0000
                Re: Text Files Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-02-26 18:50 +0300
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-26 09:26 -0800
                Re: Text Files scott@slp53.sl.home (Scott Lurndal) - 2018-02-26 19:46 +0000
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-27 09:04 +0100
                Re: Text Files already5chosen@yahoo.com - 2018-02-27 00:56 -0800
                Re: Text Files Ian Collins <ian-news@hotmail.com> - 2018-02-27 22:07 +1300
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-27 14:37 +0100
                Re: Text Files already5chosen@yahoo.com - 2018-02-27 07:38 -0800
                Re: Text Files supercat@casperkitty.com - 2018-02-27 09:18 -0800
                Re: Text Files David Brown <david.brown@hesbynett.no> - 2018-02-27 10:15 +0100
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-27 08:53 -0800
                Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-26 23:14 -0800
                Re: Text Files Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-02-26 08:32 -0700
                Re: Text Files Keith Thompson <kst-u@mib.org> - 2018-02-26 09:17 -0800
                Re: Text Files Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-26 22:55 -0800
      Re: Text Files Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 15:02 +0300
        Re: Text Files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-25 20:12 +0000

csiph-web