Thread: trouble understanding file option and command line arguments

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    trouble understanding file option and command line arguments

    Hi,

    I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read.

    this is the code I have so far:

    Code:
    /* wc simulate */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    char *pgmname; /* name of this program */
    
    int line_count = 0;
    int word_count = 0;
    int byte_count = 0;
    
    FILE *fp;
    
    void main(argc, argv)
    
    int argc; char *argv[];
    {
    
    
    int i;
    char *cp;
    
    pgmname = argv[0];
    fp = stdin;
    
    for(i = 1; i < argc; i++) {
            cp = argv[i];
            if(*cp == '-'){
                    if(*++cp == '\n'){
                            line_count++;}
                    
                    
            }
    
            else {
                      if(fp != stdin) {
                       fprintf(stderr, "%s: too many arguments\n", pgmname);
                       exit(1);
                    }
    
                    fp = fopen(cp, "r")
                    if(fp == NULL) {            
                       fprintf(stderr, "%s: unable to read %s\n", pgmname, cp);
                       exit(1);
                    }
    
            }
    
                    printf("%d\n", line_count);
    }
    
                    
    
    }
    Right now I have code to just count the lines. I am testing the program on a hello world program below:

    Code:
    #include <stdio.h>
    
    main()
    {
    printf("Hello World!\n");
    }
    when I test the program, I get 0 for line count.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Use getopt() in unistd.h, it takes care of argument parsing and gives more flexibility than doing it by hand.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsemel View Post
    Hi,

    I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read.

    Code:
    /* wc simulate */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    char *pgmname; /* name of this program */
    
    int line_count = 0;
    int word_count = 0;  <--- these don't need to be global
    int byte_count = 0;          move them inside main()
    FILE *fp;
    
    
    
    void main(argc, argv)  <--- int main (int argc, char* argv[])
    
    int argc; char *argv[];  <--- delete this
    
    {
    The remainder of the code is not very well thought out...

    You need to designate one of the argv[] strings as the filename to be parsed. From there fopen() the file, use fgets() to read it in line by line and do whatever you need to do to the text.

    You don't actually care what the name of your program is.

    Detecting flags is pretty easy...
    Code:
    if (argv[2][0] =='-')
      flag = argv[2][1]);
    from there you just parse your flags or remember them to use in conditionals in your program..

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    thank you for your reply.

    does argv need to be intialized as a 2 dimensional array?

    argv[2][0] is 2 the argument number and 0 the current line?

    thanks again!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsemel View Post
    thank you for your reply.

    does argv need to be intialized as a 2 dimensional array?

    argv[2][0] is 2 the argument number and 0 the current line?

    thanks again!
    You don't initialize argc and argv to anything... they are passed into your main() function by the operating system. To initialize them would be to destroy their contents.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    Detecting flags is pretty easy...
    Code:
    if (argv[2][0] =='-')
      flag = argv[2][1]);
    from there you just parse your flags or remember them to use in conditionals in your program..
    It kind of depends, let's say you have 10 flags where half of them takes an argument, and you want to be able to use them in any order. Then it makes sense to look into getopt() IMO, it's a good function to learn if you plan to create command line tools on *nix platforms IMO. Especially if you also want to incorporate them in shell scripts, then you would want them to behave like the rest. Perhaps it's not absolutely necessary in this case but, it's still a good function to know about.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    I have this code that I was working on before, and left out from my first post. (I'm new to this)

    Code:
    #include <stdio.h>
    
    short bflag = 0;
    short xflag = 0;
    
    FILE *fp;
    
    void main(int argc, char *argv[])
    {
    
    int i; char *cp;
    
    fp = stdin;
    
    for(i =1; i < argc; i++) {
    
    cp = argv[i];
    if (*cp == '-') {   /* a flag */
       
             if(*++cp == 'b')
                  bflag++;
    
             else if(*++cp = 'x')
                    xflag++;
    }
    I've seen programs with switch statements where there are cases with letters. I don't know where the case letters come from.

    What do you do when the flag letters are added up?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Where are you getting this garbage code from?

    I think what you really need to do is spend some time with your text book and read up on command line arguments... Your approach to this is just wrong.

    Command Line Arguments in C - Cprogramming.com

    What do you do when the flag letters are added up?
    Well in this case, I'd sing Cumbia and make s-mores...

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    I appreciate your help CommonTater. My books have not been helpful at all with accessing command line arguments. Are there any texts you could recommend? thanks!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If only we had a thread full of C Book Recommendations.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Hi CommonTater, I am not being sarcastic. In case it came off that way. I have A Book On C and Unix System Programming by Haviland and accessing command line arguments are not discussed.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    I have a Book On C by Ira Pohl and the Unix System programming. There just okay but they don't cover this.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsemel View Post
    I appreciate your help CommonTater. My books have not been helpful at all with accessing command line arguments. Are there any texts you could recommend? thanks!
    Did you by any remote chance notice the red letters in my message? That's a link... click on it!

    And then there's... this
    Last edited by CommonTater; 09-24-2011 at 05:09 PM.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    Duh!

    Oh by the way, I got that garbage code from my Professor!

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dsemel View Post
    Duh!

    Oh by the way, I got that garbage code from my Professor!
    I think you don't want to hear my response to that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with command-line arguments in C code (Linux)
    By NumLock in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 05:31 PM
  2. reading in command line arguments from a file?
    By g1i7ch in forum C Programming
    Replies: 30
    Last Post: 06-22-2006, 01:35 PM
  3. Command line arguments and file redirection
    By Abdi in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2002, 12:44 AM
  4. Command line arguments for file
    By gagig in forum C Programming
    Replies: 7
    Last Post: 02-05-2002, 02:06 AM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM