numerierung.c
changeset 0 af501b0c1716
child 8 96d16dfe787a
new file mode 100644
--- /dev/null
+++ b/numerierung.c
@@ -0,0 +1,62 @@
+/**
+ *     $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;
+}