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


Groups > comp.lang.c > #386039

Re: Whaddaya think?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Whaddaya think?
Date 2024-06-16 13:54 -0700
Organization None to speak of
Message-ID <87msnk38y1.fsf@nosuchdomain.example.com> (permalink)
References <666ded36$0$958$882e4bbb@reader.netnews.com> <87ed8x4zjl.fsf@nosuchdomain.example.com> <666f10b7$0$1412896$882e4bbb@reader.netnews.com>

Show all headers | View raw


DFS <nospam@dfs.com> writes:
> On 6/15/2024 6:22 PM, Keith Thompson wrote:
>> DFS <nospam@dfs.com> writes:
>>> I want to read numbers in from a file, say:
>>>
>>> 47 185 99 74 202 118 78 203 264 207 19 17 34 167 148 54 297 271 118
>>> 245 294 188 140 134 251 188 236 160 48 189 228 94 74 27 168 275 144
>>> 245 178 108 152 197 125 185 63 272 239 60 242 56 4 235 244 144 69 195
>>> 32 4 54 79 193 282 173 267 8 40 241 152 285 119 259 136 15 83 21 78 55
>>> 259 137 297 15 141 232 259 285 300 153 16 4 207 95 197 188 267 164 195
>>> 7 104 47 291
>>>
>>>
>>> This code:
>>> 1 opens the file
>>> 2 fscanf thru the file to count the number of data points
>>> 3 allocate memory
>>> 4 rewind and fscanf again to add the data to the int array
>>>
>>>
>>> Any issues with this method?
>>>
>>> Any 'better' way?
>>>
>>> Thanks
>> In a quick test, your code compiles without errors and runs
>> correctly
>> with your input.  I do get a warning about argc being unused, which you
>> should address.
>
> -Wall doesn't warn about that, but -Wall -Wextra does.

True (at least for gcc).

> In the bigger program of which this is a part, argc IS used.

I suggest that argc *should* have been used in this program.

>>> ----------------------------------------------------------
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> int main(int argc, char *argv[]) {
>>>
>>> 	int N=0, i=0, j=0;
>> The usual convention is to use all-caps for macro names.  Calling
>> your
>> variable N is not a real problem, but could be slightly confusing.
>> N is the number of integers in the input.  i is an index.  j is a
>> value
>> read from the file.  That's not at all clear from the names.
>> I suggest using longer and more descriptive names in lower case.
>> "N" could be "count".  "i" is fine for an index, but "j" could be
>> "value".
>
>
> N is used in statistics, and this is a stats program.

OK, using the names i and j suggests they're used similarly when they're
not.

[...]

> I looked for an easy way to lock it while reading, but as I understand
> flock() it only places an 'advisory lock' on the file, and other 
> processes are still free to modify it.

Locking the file probably isn't worth the effort.  If I were doing this
kind of thing, I'd just keep in the back of my mind that if some other
process modifies the file while my program is running, bad things can
happen.  For something that's going to be used in production, more care
is appropriate.

[...]

>> A minor style point: a return statement doesn't require parentheses.
>> IMHO using parentheses make it look too much like a function call.  I'd
>> write `return 0;`, or more likely I'd just omit it, since falling off
>> the end of main does an implicit `return 0;` (starting in C99).
>
> Can't omit it.  It's required by my brain.

OK, that's harmless -- but be prepared to see the "return 0;" omitted in
code written by other people.

>
>>> }
>> A method that doesn't require rescanning the input file is to
>> initially
>> allocate some reasonable amount of memory, then use realloc() to
>> expand the array as needed.  Doubling the array size is probably
>> reasonable.  It will consume more memory than a single allocation.
>
> Done in a way, as you'll see below.
>
>
> Thanks for the thorough analysis and good tips.
>
>
> Updated
> * dropped 2 variable declarations
> * allocate 'on the fly'
> * one fscanf thru the file
> * 4 less lines of code (not incl brackets)
>
> ----------------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[]) {
>
>     int N=0;
>     int *nums = malloc(2 * sizeof(int));

Again, I really recommend

    int *nums = malloc(2 * sizeof *nums);

>     FILE* datafile = fopen(argv[1], "r");

Undefined behavior if the program is invoked with no arguments.
No check whether fopen succeeded.

>     while(fscanf(datafile, "%d", &nums[N++]) == 1){

I've already discussed the problems of using fscanf for numeric input.

>         nums = realloc(nums, (N+1) * sizeof(int));

I'd think about moving the increment of N so that I don't have to use
"N+1" in realloc or add "N--;" at the end.  I haven't taken the time to
work out the details, but I think this can be done more cleanly.

Calling realloc() every time could be wasteful.  (On my system, realloc
reallocates at about 25 bytes and not again until about 128 kbytes, but
don't rely on that.)  A common scheme is to call realloc() to double the
buffer size as needed.  This requires keep track of the size of the
buffer and the number of elements used separately.  But for a
quick-and-dirty demo, calling realloc() every time isn't too bad.

>     }
>     fclose (datafile);
>
>     N--;
>     for(int i=0;i<N;i++) {
>         printf("%d.%d  ", i+1, nums[i]);
>     }
>     free(nums);
>
>     printf("\n");
>     return 0;
>
> }
> ----------------------------------------------------------

You changed the output format.  Your original program printed each
number on line line (with extra blank lines at the top and bottom for
some reason); this program prints all the numbers on a single line.
Neither is right or wrong, but consistency is nice -- and very long
lines can cause problems in looking at the output.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

Whaddaya think? DFS <nospam@dfs.com> - 2024-06-15 15:36 -0400
  Re: Whaddaya think? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-15 22:33 +0100
  Re: Whaddaya think? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-15 23:03 +0100
    Re: Whaddaya think? bart <bc@freeuk.com> - 2024-06-16 00:22 +0100
      Re: Whaddaya think? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-16 10:30 +0100
    Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-16 11:52 -0400
      Re: Whaddaya think? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-17 00:17 +0100
        Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 08:49 -0400
  Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-15 15:22 -0700
    Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-16 12:20 -0400
      Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-16 13:54 -0700
      Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-16 22:41 -0400
        Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-16 22:45 -0700
        Re: Whaddaya think? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-17 07:39 +0000
          Re: Whaddaya think? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-06-17 01:22 -0700
          Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 09:50 -0400
            Re: Whaddaya think? Richard Harnden <richard.nospam@gmail.invalid> - 2024-06-17 16:23 +0100
              Re: Whaddaya think? David Brown <david.brown@hesbynett.no> - 2024-06-17 18:46 +0200
              Re: Whaddaya think? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-22 22:14 +0000
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-22 17:10 -0700
                Re: Whaddaya think? Phil Carmody <pc+usenet@asdf.org> - 2024-06-23 11:20 +0300
          Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-18 00:19 -0700
            Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 03:10 -0700
              Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-18 17:24 -0700
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 17:55 -0700
                Re: Whaddaya think? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-19 01:58 +0000
        Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 08:50 -0400
          Re: Whaddaya think? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-17 15:41 +0100
            Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-18 08:12 +0200
              Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 03:07 -0700
      Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-17 00:30 -0400
  Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-16 01:56 +0300
    Re: Whaddaya think? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-16 03:26 +0000
      Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 05:41 +0200
        Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-15 21:17 -0700
          Re: Whaddaya think? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-16 04:41 +0000
          Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 06:44 +0200
            Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-16 11:09 -0400
              Re: Whaddaya think? David Brown <david.brown@hesbynett.no> - 2024-06-16 17:56 +0200
                Re: Whaddaya think? bart <bc@freeuk.com> - 2024-06-16 18:14 +0100
                Re: Whaddaya think? David Brown <david.brown@hesbynett.no> - 2024-06-17 14:44 +0200
                Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 09:52 -0400
        Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 06:51 +0200
          Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-15 22:21 -0700
            Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 07:41 +0200
              Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-15 22:49 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 11:29 +0200
                Re: Whaddaya think? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-16 16:04 +0100
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 17:13 +0200
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-16 13:32 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 07:41 +0200
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-16 23:20 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 09:16 +0200
                Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-17 09:38 -0400
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-17 16:17 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-18 07:09 +0200
                Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-18 03:25 -0400
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 02:57 -0700
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-17 16:15 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-18 08:02 +0200
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 19:07 -0700
                Re: Whaddaya think? David Brown <david.brown@hesbynett.no> - 2024-06-19 09:50 +0200
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-19 13:13 -0700
                Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-17 02:21 -0400
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 09:22 +0200
              Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-16 11:11 +0300
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 11:07 +0200
                Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-16 12:38 +0300
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 12:03 +0200
                Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-16 14:31 +0300
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 17:37 +0200
                Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-17 00:45 +0300
              Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-16 17:06 -0700
                Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-16 22:40 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 07:52 +0200
                Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 09:45 -0400
                Re: Whaddaya think? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-06-17 13:16 -0700
                Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-17 17:07 -0400
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-17 15:48 -0700
                Re: Whaddaya think? scott@slp53.sl.home (Scott Lurndal) - 2024-06-17 22:48 +0000
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-17 15:44 -0700
                Re: Whaddaya think? David Brown <david.brown@hesbynett.no> - 2024-06-18 15:00 +0200
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-18 06:57 +0200
                Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-18 00:25 -0700
                Re: Whaddaya think? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-17 02:38 -0400
                Re: Whaddaya think? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-18 17:01 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 07:48 +0200
                Re: Whaddaya think? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-16 23:29 -0700
                Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-17 09:35 +0200
        Re: Whaddaya think? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-16 08:19 +0100
        Re: Whaddaya think? Michael S <already5chosen@yahoo.com> - 2024-06-16 10:44 +0300
          Re: Whaddaya think? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-16 11:13 +0200
    Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-16 11:03 -0400
  Re: Whaddaya think? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-16 15:52 +0000
    Re: Whaddaya think? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-16 17:17 +0100
    Re: Whaddaya think? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-18 08:06 +0000
    Re: Whaddaya think? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-25 17:37 +0000
      Re: Whaddaya think? DFS <nospam@dfs.com> - 2024-06-25 14:09 -0400
        Re: Whaddaya think? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-25 18:11 +0000

csiph-web