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


Groups > comp.lang.c > #100095

Re: Piece of code to read and process data from a file, separating chars and ints

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Piece of code to read and process data from a file, separating chars and ints
Date 2016-12-23 14:10 -0800
Organization None to speak of
Message-ID <lnfuleb8le.fsf@kst-u.example.com> (permalink)
References <0fb61ff9-91e4-4001-b99d-6cc1ce28dc6f@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> writes:
[...]
> (2) Attn. Richard: I have disobeyed you, Sir ) My apologies. I have
> implemented the dynamic memory allocation to follow one of the
> headsups suggested by Keith.  This is the only one feature that is
> “sort of” more advanced, but I do believe it is necessary for this
> program, not only because the lines might be arbitrarily long, but
> because it is a great practice, and it is not such a fancy feature
> after all.

I'm sorry if my advice led you astray.

What I meant to suggest (and I think I did, but I'm too lazy to
go back and check) is that if you want to handle arbitrarily long
lines, you should implement a function to do that *separately*,
not as part of your large CSV-processing program.

For example, you might write a function:

    char *get_line(FILE *in);

(Note: not "getline", there's a POSIX function by that name) that
returns a pointer to an allocated string.  Internally, get_line()
would probably use fgets(), malloc(), and realloc().

Implement and debug that function *separately*, using a program
that does nothing but call get_line() to read lines from a file.
Once you've tested it with as many cases as you can think of (empty
lines, short lines, very long lines, multiple calls, correctly
handling EOF, correctly handling a trailing line with no '\n'),
*then* you can drop that function into your CSV program.

As it is, you haven't even written a separate line-reading function.
You've created some wrappers around your realloc() calls (which
I'm not sure was a useful thing to do), but your allocation logic
is mixed up with your input loop.

What I think you *should* have done was write your CSV program to
use fgets(), handling lines up to, say, 1000 characters, and test it
with input data that meets that restriction.  And once that works,
you could drop in your get_line() function if you insist on doing
that extra work.

As it is, you have something going wrong that's causing realloc()
to fail -- and it's going to be difficult to track down that error.

Another observation (I haven't read your entire program): Several of
your functions return EXIT_FAILURE and EXIT_SUCCESS to indicate whether
they failed or succeeded.  That might seem like a good idea, but in my
opinion it's poor style.  Those values specify the return status of your
main program.  A common convention is for a function to return an int
status, with 0 denoting success and non-0 denoting failure (note that
EXIT_SUCCESS is not necessarily 0).  Or your functions could return bool
if you don't need to distinguish among different kinds of failures.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 05:02 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints Richard Heathfield <rjh@cpax.org.uk> - 2016-12-23 14:28 +0000
    Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 06:39 -0800
      Re: Piece of code to read and process data from a file, separating chars and ints Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-23 16:29 +0000
        Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 08:44 -0800
          Re: Piece of code to read and process data from a file, separating chars and ints Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-23 20:31 +0000
            Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 20:52 -0800
      Re: Piece of code to read and process data from a file, separating chars and ints Richard Heathfield <rjh@cpax.org.uk> - 2016-12-23 19:34 +0000
        Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 21:31 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints Keith Thompson <kst-u@mib.org> - 2016-12-23 14:10 -0800
    Re: Piece of code to read and process data from a file, separating chars and ints Alla _ <modelling.data@gmail.com> - 2016-12-23 21:09 -0800
  Re: Piece of code to read and process data from a file, separating chars and ints "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2016-12-24 14:49 +0000

csiph-web