[Solved] What are the advantages of command line arguments in C? [closed]


Let’s say you’ve a command line file_matcher program, how are you going to use it? well, you’ve to pass the file names to the program that you want to match. You can not hard code them(means, you can not write the file names in the code file). cause if you hard code them, then you’ve to change the file names in the program code, each time, if for any reason the file name changes or you want to test new files.

Anyway I’m giving a piece of code I’ve written a long time ago to match two files. Play with it. And to run it call it like(assuming two files file_1.txt and file_2.txt are in the same dir as file_matcher):

> file_matcher file_1.txt file_2.txt

And file_matcher.c:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>


char *get_charname(int ch) {
    if(ch == 0) return "Null";
    if(isspace(ch)) return "White space";
    if(iscntrl(ch)) return "Control Key";
    return "Others";
}

int main(int argc, char *argv[]) {
    FILE *f1, *f2;
    char ch1, ch2;
    int row, col;
    
    if(argc != 3) {
        if(argc == 1) printf("Missing file names\n");
        else if(argc == 2) printf("Missing a file name\n");
        else printf("Too many files\n");
        exit(1);
    }

    f1 = fopen(argv[1], "r");
    if(f1 == NULL) {
        printf("Error opening \"%s\"\n", argv[1]);
        exit(1);
    }
    f2 = fopen(argv[2], "r");
    if(f2 == NULL) {
        printf("Error opening \"%s\"\n", argv[2]);
        fclose(f1);
        exit(1);
    }
    
    row = 1;
    col = 1;
    while(fscanf(f1, "%c", &ch1) != EOF) {
        if(fscanf(f2, "%c", &ch2) == EOF) {
            printf("Too less data in \"%s\"\n", argv[2]);
            printf("Matching stopped at (row, col): (%d, %d)\n", row, col);
            fclose(f1);
            fclose(f2);
            exit(1);
        }
        
        if(ch1 != ch2) {
            printf("Miss match at (row, col): (%d, %d)\n", row, col);
            
            if(isgraph(ch1)) printf("In \"%s\" found '%c'(ascii value: %d)\n", argv[1], ch1, ch1);
            else printf("In \"%s\" found '%s'(ascii value: %d)\n", argv[1], get_charname(ch1), ch1);
            
            if(isgraph(ch2)) printf("In \"%s\" found '%c'(ascii value: %d)\n", argv[2], ch2, ch2);
            else printf("In \"%s\" found '%s'(ascii value: %d)\n", argv[2], get_charname(ch2), ch2);
            
            fclose(f1);
            fclose(f2);
            exit(1);
        }
        
        if(ch1 == '\n') {
            row++;
            col = 1;
        }
        else col++;
    }
    if(fscanf(f2, "%c", &ch2) != EOF) {
        printf("Too much data in \"%s\"\n", argv[2]);
        printf("Matching stopped at (row, col): (%d, %d)\n", row, col);
        fclose(f1);
        fclose(f2);
        exit(1);
    }
    
    printf("\"%s\" and \"%s\" matched succesfully\n", argv[1], argv[2]);
    
    fclose(f1);
    fclose(f2);
    
    return 0;
}

Like, this program, many other programs are designed to take input from command line…

I hope, now you understand the reason behind it….

2

solved What are the advantages of command line arguments in C? [closed]