numerierung.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:54 +0100
changeset 3 820ed7fb9314
parent 0 af501b0c1716
child 8 96d16dfe787a
permissions -rw-r--r--
database, gauss, lotto, mem2swap, prog_limit moved to demos committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *     $Id: numerierung.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
 * $Source: /development/c/demos/numerierung.c,v $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>

void usage (char *cmd)
{
    printf ("Usage: %s [-h] [-t] [-v] [file]\n", cmd);
    exit (0);
}

int main (int argc, char **argv)
{
    char buffer[81];
    int counter = 0;
    FILE *f;
    int c, pure = 0;

    while ((c = getopt (argc, argv, "hp")) != -1) {
        printf ("Zeichen: %c\n", c);
        switch (c) {
        case 'h':
            usage (argv[0]);
            break;
        case 'p':
            pure = 1;
            break;
        default:
            printf ("Unknown Parameter: %s\n", argv[optind]);
        }
        printf ("GETOPT: %d : %s\n", optind, argv[optind]);
    }

    if (optind == argc)
        f = stdin;
    else
        f = fopen (argv[optind], "r");

    if (!f) {
        perror ("Error opening file");
        return EXIT_FAILURE;
    }

    *buffer = 0;

    while (fgets (buffer, 80, f) != NULL) {
        if (!pure)
            printf ("%4.4d: %s", counter++, buffer);
        else
            printf ("%s", buffer);
        *buffer = 0;
    }

    fclose (f);
    return EXIT_SUCCESS;
}