简体   繁体   中英

How to get Command Line Arguments running in C for input and output files

I am trying to run an application in the CMD developer prompt, it should have an input file and output file using command line arguments (which i don't understand, or can't wrap my head around). But when I do it, it's running and ending but nothing is being printed to the output file.

Below is my code with non-relevant code redacted. I don't know if I'm doing something wrong in the code or the cmd line developer prompt.

The app is called printlines.exe and my command looks like this:

 printlines.exe -i file.java -o output.txt

Any advice would be greatly appreciated.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define LINE 1000
void countLines(int *emptyLines, int *totalLines, int *totalComments, char *fileName, FILE *readFile)
{
    char line[LINE]; // set array to store input file data
    //set variables
    readFile = fopen(fileName, "r");
    int lines = 0;
    int comments = 0;
    int empty = 0;

    while (fgets(line, LINE, readFile) != NULL) //while loop that reads in input file line by line
    {
        /*counts lines in input file works when not using 
            command line arguments redacted to shorten */
    }

    fclose(readFile);

    *emptyLines = empty;
    *totalLines = lines;
    *totalComments = comments;
}
int main(int argc, char *argv[])
{
    FILE *inputFile;
    FILE *outputFile;
    char *outputName;
    char *inputName;

    inputName = argv[1];
    outputName = argv[2];

    inputFile = fopen(inputName, "r");
    outputFile = fopen(outputName, "w");

    int emptyLines, totalLines, totalComments;
    countLines(&emptyLines, &totalLines, &totalComments, inputName, inputFile);
    int character;
    bool aComment = false;

    while ((character = fgetc(inputFile)) != EOF)
    {
        /* code that writes info from input to output, works 
              when not using command line arguments redacted to shorten*/
    }

    //close files
    fclose(inputFile);
    fclose(outputFile);

    printf("There are %d total lines, %d lines of code and %d comments in the file\n", totalLines, emptyLines, totalComments);
    return 0;
}

No error checking is being performed, neither for command line arguments nor for errors opening the files. I'm willing to bet you would quickly pinpoint the problem if you did the following:

//....
if(argc < 5){ // check arguments
    fprintf(stderr, "Too few arguments");
    return EXIT_FAILURE;
}

inputFile = fopen(inputName, "r"); 
if(inputFile == NULL){ // chek if file was open
    fprintf(stderr, "Failed to open input file\n");
    return EXIT_FAILURE;
}
outputFile = fopen(outputName, "w");
if(outputFile == NULL){ // again
    fprintf(stderr, "Failed to open output file\n");
    return EXIT_FAILURE;
}
//...

Note that your comand line string has 5 arguments, and the file names are at indexes 2 and 4 so you would need:

inputName = argv[2];
outputName = argv[4];

Or just remove the -i and -o since they don't seem to be doing anything.

I should also note that you can use command line arguments directly on fopen or in your function, there is no need for the two extra pointers:

inputFile = fopen(argv[2], "r");
outputFile = fopen(argv[4], "w");
//...
countLines(&emptyLines, &totalLines, &totalComments, argv[2], inputFile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM