1

Hello I have a project I'm doing and I need my program to run from the command line and be able to read flags and file names that will be used in the program.

This is my current code. It compiles without entering any flags. I don't think my GetArgs does anything. I had help with that part of the code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024
#define IN 1 /* inside a word */ 
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */

int numInputArgs;
int idx;
void GetArgs (int argc, char **argv){

for (idx = 1; idx < 4;  idx++) {
    if (strcmp(argv[idx], "-c") == 0) {
        printf("Flag -c passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-w") == 0) {
        printf("Flag -w passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-l") == 0) {
        printf("Flag -l passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-L") == 0) {
        printf("Flag -L passed\n");
        break;
    }
    else {
        printf("Error: unknown flag\n");
        exit(-1);
    }
}
 }// end GetArgs

void lineWordCount ( ) {

int c, nl, nw, nc, state;


    state = OUT; nl = nw = nc = 0; 
    while ((c = getchar()) != EOF) {
            ++nc;

        if (c == '\n')
            ++nl; 

        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT; 

        else if (state == OUT) {
            state = IN; ++nw;
        } 
        printf("%d %d %d\n", nl, nw, nc);
    }
 }// end lineWordCount








 int main(int argc, char **argv){

GetArgs(argc, argv);
lineWordCount();
printf("Hello");

//fclose( src );
}
4
  • FYI: homework tag is deprecated. Commented Sep 18, 2012 at 5:44
  • You're right, all GetArgs does is print the arguments. If you are on a POSIX machine (like Linux or Mac OSX) I suggest you look to getopt. There are also many arguments parsing libraries available if you search, the above mentioned getopt function exists for Windows as well for example. Commented Sep 18, 2012 at 5:48
  • Your GetArgs does perfectly what it should: $ gcc -o issue issue.c $ ./issue.exe a b c abc So, what exactly is your question? Commented Sep 18, 2012 at 5:50
  • I need the GetArgs to be able to scan for four potentially flags that decide what information is displayed at the end. before processing the files. Commented Sep 18, 2012 at 5:51

2 Answers 2

4

You can either use a standard function like getopt() as mentioned by @Joachim, if it is available on your system, or you can code it yourself. If you have a complicated command line syntax, getopt() might be better suited - if you only need to check for a limited set of flags, it might be easier to code it yourself, for example:

void GetArgs (int argc, char **argv){
   int idx = 0;

   for (idx = 1; idx < argc;  idx++) {
       if (strcmp(argv[idx], "-a") == 0) {
          printf("Flag -a passed\n");
       } else if (strcmp(argv[idx], "-b") == 0) {
          printf("Flag -b passed\n");
       } else if (strcmp(argv[idx], "-c") == 0) {
          printf("Flag -c passed\n");
       } else {
          printf("Error: unknown flag %s\n");
       }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is there something wrong with my main.. It isn't printing anything else besides what's in lineWordCount
lineWordCount() is not called at all in your code above. I took your code and passed it to the compiler unmodified, and when I call the programm with several command line arguments it prints each of them, exactly what I would expect ...
I added it right under GetArgs in main and I'm getting stuck in the GetArgs. Trying to mess with it so it will get to lineWordCount. I'll update the code right now.
I figured out why. I was using UNIX built in "wc" (word count) and I thought it was my program functioning. :(
In the unix shell, always call your program with ./ prepended to avoid such issues. Also, avoid using existing names (you can check with which or where)
|
0

I recommend you to use argtable2 library. I have used it for a long time and I think it is great. There are tutorials available to see how powerful and easy to use it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.