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


Groups > comp.lang.c > #99496

Re: Program to read csv file, store characters and numbers in separate arrays

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Program to read csv file, store characters and numbers in separate arrays
Date 2016-12-15 14:12 +0000
Organization Fix this later
Message-ID <o2u8a9$3n0$1@dont-email.me> (permalink)
References <09988b6c-e11e-4fd6-8731-7992d16c7b94@googlegroups.com>

Show all headers | View raw


[the code]

On 15/12/16 13:05, Alla _ wrote:

> (3) I will use a header file, in which I will store preprocessor commands, like
> #define and #include, as well as functions' declarations, etc.;

A well-designed header contains *only* the following:

1) inclusion guards;
2) macro definitions (#defines);
3) type definitions (typedefs);
4) function prototypes;
5) comments;
6) #include directives *ONLY* for those headers that are required for 
the immediate purposes of the header itself.

For example, here's a quick header I threw together a year or so ago:

/* CISPRNG - Cryptographically In-Secure
  * Pseudo-Random Number Generator
  *
  * Written by Richard Heathfield,
  * 20 October 2015.
  *
  * You will also need cisprng.c and a
  * C compiler.
  *
  * The author places this code into the
  * public domain, and so it may be freely
  * used for any legal purpose, although
  * I would recommend against using it
  * in circumstances where you really need
  * a cryptographically secure PRNG, because
  * it isn't one.
  */
#ifndef H_CISPRNG_H_
#define H_CISPRNG_H_

#define CISPRNG_RAND_MAX (0x7FFFFFFFUL)

/* Single state versions */
/* set the seed */
void cisprng_set(unsigned long seed);
/* get a PRN */
unsigned long cisprng_random(void);
/* get a PRN in a range */
unsigned long cisprng_range(unsigned long low,
                             unsigned long high);
/* Custom state versions */
unsigned long cisprng_csrandom(unsigned long *pseed);
unsigned long cisprng_csrange(unsigned long *pseed,
                               unsigned long low,
                               unsigned long high);
#endif
/* end of cisprng.h */

Note that it doesn't define any types. But if it did, they would go 
after the #defines but before the function prototypes. Nor does it 
include any headers. But *if* I'd used, say, size_t *in the header*, I'd 
have included <stddef.h> in the header. But I would *not* include 
<stddef.h> in the header just because the corresponding C file used 
size_t (which, actually, it doesn't). A module header is not a place to 
hide #includes.

> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
> #include <string.h>
>
>
> #define BUFFER_SIZE 100
> //#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
> #define INCOME_STATEMENT "files/income_test.csv"
>
> int main(int argc, char *argv[])
> {
>     //check for correct number of arguments
>     if(argc > 2)
>     {
>         printf("Usage: program_name [file_name]");

You might want a \n on there. Or use puts() instead if you prefer.

Otherwise, this happens:

Usage: program_name [file_name]me@mymachine$

which probably isn't what you wanted to see.

>         return EXIT_FAILURE;
>     }
>     //determine a file to use
>     char *data_file = (argc == 2)? argv[1] : INCOME_STATEMENT;

I don't suppose you plan to change the filename, so why not make this const?

const char *data_file = (argc == 2)? argv[1] : INCOME_STATEMENT;

This will remove the *only* diagnostic message that gcc gave me (and by 
the way, to get only one warning when using my flag choices is quite an 
achievement!).

What should the program do if there are several arguments? At the 
moment, if I tell it:

./alla test1.csv test2.csv

I get this message:

Usage: program_name [file_name]

If the program can only handle one data file, I would expect to see 
something like:

Sorry, I can only process one file. Ignoring subsequent arguments...

which you would code like this:

const char *data_file = (argc > 1)? argv[1] : INCOME_STATEMENT;
if(argc > 2)
{
   fputs("Sorry, I can only process one file."
         " Ignoring subsequent arguments...\n",
         stderr);
}

>
>     FILE *fp = fopen(data_file, "r");
>
>     if ( fp )
>     {
>         char buffer[BUFFER_SIZE] = "";
>         char array_names[50][50];
>         int  array_values[50][50];

I am taking you at your word and forbearing to comment on these values, 
but for the sake of a predictable life I would suggest that you 
initialise these two arrays:

char array_names[50][50] = {0};
int  array_values[50][50] = {0};

which will take advantage of the default static initialisation rule to 
set everything to 0.

>         char *ptr_buffer = NULL;
>         size_t i, j, k;
>
>         //Read each line from the file, store in the buffer
>         for ( i = 0; fgets(buffer, sizeof(buffer), fp) != NULL; ++i )

Since i isn't part of the loop control logic, I would suggest that you 
replace this line with the much more usual:

   i = 0;
   while(fgets(buffer, sizeof buffer, fp) != NULL)
   {
     /* stuff */
     i++;
   }

And if i is a line number, why not call it line_number? That's much more 
descriptive than i.

The loop body looked more or less okay to me, except that it's a touch 
on the large side. Think about functional decomposition.

>         if (!feof(fp))
>         {
>             puts("Something went wrong with the provided file\n");
>             return EXIT_FAILURE;
>         }

Why not use ferror(fp) instead? It's a more direct description of what 
you're trying to find out:

   if(ferror(fp))
   {

And while we're on the subject, diagnostic messages sit much better on 
stderr than on stdout. That way, they can be separated from the 
program's normal output, like this:

./alla test1.csv > test1.out 2> test1.log

which will put stdout stuff into test1.out and stderr stuff into test1.log.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

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


Thread

Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 05:05 -0800
  Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 13:48 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:22 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:28 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 09:25 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 19:31 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:19 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:01 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 02:43 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:08 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays jameskuyper@verizon.net - 2016-12-18 03:32 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Barry Schwarz <schwarzb@dqel.com> - 2016-12-16 17:09 -0800
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:25 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:11 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 02:45 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:54 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:56 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 18:02 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:06 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:28 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 12:02 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 12:41 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 13:40 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 14:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 13:03 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 15:04 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-16 15:26 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 17:01 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 04:18 -0800
  Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:12 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 16:14 +0100
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 15:37 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 23:20 +0100
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 23:27 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 12:10 +0100
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:43 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 14:01 +0100
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 13:12 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-16 14:57 +0100
      Re: Program to read csv file, store characters and numbers in separate arrays BartC <bc@freeuk.com> - 2016-12-15 16:30 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:36 +0000
          Re: Program to read csv file, store characters and numbers in separate arrays David Brown <david.brown@hesbynett.no> - 2016-12-15 23:29 +0100
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:26 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:33 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-15 08:32 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 16:37 +0000
      Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-15 12:17 -0800
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:33 -0800
    Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-15 12:23 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 20:39 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 21:13 +0000
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 00:39 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 08:54 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:09 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 09:32 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:51 -0800
              Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:08 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 11:09 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:16 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:20 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:48 -0800
                Re: Program to read csv file, store characters and numbers in separate arrays gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 17:55 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays raltbos@xs4all.nl (Richard Bos) - 2016-12-20 11:53 +0000
                Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:33 +0000
            Re: Program to read csv file, store characters and numbers in separate arrays Keith Thompson <kst-u@mib.org> - 2016-12-16 09:46 -0800
  Re: Program to read csv file, store characters and numbers in separate arrays Barry Schwarz <schwarzb@dqel.com> - 2016-12-15 12:36 -0800
    Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 01:44 -0800
      Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 10:04 +0000
        Re: Program to read csv file, store characters and numbers in separate arrays Alla _ <modelling.data@gmail.com> - 2016-12-16 03:14 -0800
          Re: Program to read csv file, store characters and numbers in separate arrays Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 11:46 +0000

csiph-web