Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Using STD lib

Useful includes

Code Block
#include <stdlib.h>
#include <string>   // for string, substring
#include <sstream>  // for streamstring
#include <iostream> // for cout, puts etc.
#include <stdio.h>  // for  sprintf, printf( "%lf\n", accum );
#include <time.h>   // time
#include <fcntl.h>  // open()
#include <unistd.h> // read()
#include <iomanip>  // for setw

using namespace std; // allows get rid of std:: prefix.

...

Code Block
#include <time.h>   // time
  struct timespec start, stop;
  int gettimeStatus = clock_gettime( CLOCK_REALTIME, &start );

  printf ( "gettimeStatus: %d \n", gettimeStatus );
  printf ( "start.tv_sec = %d  start.tv_nsec = %d \n", start.tv_sec, start.tv_nsec );

      gettimeStatus = clock_gettime( CLOCK_REALTIME, &stop );
  printf ( " stop.tv_sec = %d   stop.tv_nsec = %d \n", stop.tv_sec,  stop.tv_nsec );
  printf ( "      dt_sec = %d        dt_nsec = %d \n", stop.tv_sec - start.tv_sec, 
                                                       stop.tv_nsec- start.tv_nsec);

...

Input parameters parsification with getopt

getopt has potential problem: it does not detect the missing argument and pick-up the next available option as an argument...
RETURN VALUE:

  • The getopt() function shall return the next option character specified on the command line.
  • A colon ( ':' ) shall be returned if getopt() detects a missing argument and the first character of optstring was a colon ( ':' ).
  • A question mark ( '?' ) shall be returned if getopt() encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon ( ':' ).
  • Otherwise, getopt() shall return -1 when all command line options are parsed.
Code Block

#include <getopt.h>
#include <stdio.h>
#include <iostream>
//===================
void usage(char* name) {
  std::cout << "Usage: " << name << " [-a <aaa>] [-b <bbb>] [-c <ccc>] [-h] <p1> [<p2> [<p3> ...]] \n";
}
//===================
int main (int argc, char **argv)
{
    char *avalue = NULL;
    char *bvalue = NULL;
    char *cvalue = NULL;
    int index;
    int c;
    extern char *optarg;
    extern int optind, optopt, opterr;

    while ((c = getopt (argc, argv, ":a:b:c:h")) != -1)
        switch (c)
        {
          case 'a':
            avalue = optarg;
            printf ("a: avalue = %s\n",avalue);
            break;
          case 'b':
            bvalue = optarg;
            printf ("b: bvalue = %s\n",bvalue);
            break;
          case 'c':
            cvalue = optarg;
            printf ("c: cvalue = %s\n",cvalue);
            break;
          case 'h':
            printf ("h: ");
	    usage(argv[0]);
            break;
          case ':':
            printf ("(:) Option -%c requires an argument.\n", optopt);
	    usage(argv[0]);
            return EXIT_FAILURE;
          case '?':
            printf ("?: Option -%c requires an argument.\n", optopt);
	    usage(argv[0]);
            return EXIT_FAILURE;
          default:
            printf ("default: You should not get here... option -%c .\n", optopt);
            abort ();
        }

    printf ("End of options: avalue = %s, bvalue = %s, cvalue = %s\n",
            avalue, bvalue, cvalue);

    for (index = optind; index < argc; index++)
        printf ("Non-option argument %s\n", argv[index]);
    return EXIT_SUCCESS;
}

Iterate over files in the directory

...