Thread: command line argument help

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    command line argument help

    Code:
    //detab - replace tabs with appropriate number of blank spaces to reach next
    //tabstop. Assume a fixed number of tabstops.
    
    #include <stdio.h>
    
    #define MAXLINE 81
    
    int gotline(char s[],int lim);
    
    int main(int argc,char argv[]){
        //int len;                    /* length of current line */
        //int getmore = 0;
        int tabstops = (argv[1] - '0');     //here is the line in question
        
        //char line[MAXLINE];            /* current line being read */
        
        
        printf("%d\n",tabstops);
        getchar();
        return 0;
    }
    /*int gotline(char s[],int lim){
        int c,i;
        for(i=0;i < lim-1 && (c = getchar()) != EOF && c != '\n';++i)
            s[i] = c;
        if(c == '\n'){
            s[i] = c;
            ++i;
        }
        else if(c == EOF && i > 0){
            s[i] = '\n';
            ++i;
        }
        s[i] = '\0';
        return i;
    }*/
    commented code is not the problem... I'm trying to learn how to convert an interger entered on the command line as an argument from a char pointer to an integer data type of the same respective value, (ie argv[1] is a char pointer to an integer, say 14, and I would like to place the value "14" into "tabstops"

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You got the arguments to main listed wrong, should be:
    Code:
    int main(int argc,char *argv[])
    argv should be an array of pointers (one pointer to each command line argument) to char. You currently have an array of characters which is not the same thing. Then, assuming you enter a single character digit you'd do:
    Code:
    int tabstops = argv[1][0] - '0';
    ...this would pick up the first (and hopefully only) character of the second command line arg and convert from a char to it's integer representation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    another way would be
    Code:
    count = sscanf(argv[1],"%d",&tabstops);
    if (count != 1) {
        invalid input
    }
    this would convert any number, not just a single digit.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    thank you for your quick responses. i would need to be able to convert all characters in the second argument, not just the first one. But I also cannot use sscanf() because I am not far enough in the book to use this function.

    Code:
    if(argc != 2)
            printf("Usage: %s n [tabstop every n columns]\n",argv[0]);
        else{
            //tabstops = argv[1][0] - '0';     //converts only first char
            int c,i;
            
            for(i = 0;((c = argv[1][i]) != '\0');++i)
                tabstops += (c - '0');
            printf("%d\n",tabstops);
         }
    with this, im assuming that the char array will end with a null character(wrong assumption im guessing?). So far im getting garbage.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by jwroblewski44 View Post
    thank you for your quick responses. i would need to be able to convert all characters in the second argument, not just the first one. But I also cannot use sscanf() because I am not far enough in the book to use this function.

    Code:
    if(argc != 2)
            printf("Usage: %s n [tabstop every n columns]\n",argv[0]);
        else{
            //tabstops = argv[1][0] - '0';     //converts only first char
            int c,i;
            
            for(i = 0;((c = argv[1][i]) != '\0');++i)
                tabstops += (c - '0');
            printf("%d\n",tabstops);
         }
    with this, im assuming that the char array will end with a null character(wrong assumption im guessing?). So far im getting garbage.
    You've got the idea. First, converting a strign to an integer is a natural unit of programming. So why not wrap it up in a function.

    Code:
    int stringtointeger(char *str)
    Secondly, the logic is that you don't add the digits together. You take the first digit, subtract '0' for it to get the integer, as you have done. Then you look at the next character. If it is a digit, multiply the number you have by ten, subtract the '0', and add. Then repeat until there are no new digits waiitng. You can get the next character simply by indexing [i+1].

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by jwroblewski44 View Post
    thank you for your quick responses. i would need to be able to convert all characters in the second argument, not just the first one. But I also cannot use sscanf() because I am not far enough in the book to use this function.
    There will be a lot of other functions your books don't touch on because the point of the book is to teach you the language not necessarily the entire C library. So why not start a, let's say cheeky adventure, and start using cool functions like sscanf which mind you ALSO help you learn something new in the process .
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by claudiu View Post
    There will be a lot of other functions your books don't touch on because the point of the book is to teach you the language not necessarily the entire C library. So why not start a, let's say cheeky adventure, and start using cool functions like sscanf which mind you ALSO help you learn something new in the process .
    *le gasp* Such scandalous audacity!
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Quote Originally Posted by Malcolm McLean View Post
    You've got the idea. First, converting a strign to an integer is a natural unit of programming. So why not wrap it up in a function.

    Code:
    int stringtointeger(char *str)
    Secondly, the logic is that you don't add the digits together. You take the first digit, subtract '0' for it to get the integer, as you have done. Then you look at the next character. If it is a digit, multiply the number you have by ten, subtract the '0', and add. Then repeat until there are no new digits waiitng. You can get the next character simply by indexing [i+1].
    thank you for the advice on multiplying by ten and adding the digit. Is there a specific char I can look for to notify the end of the string? Obviously '\0' is not the choice.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by jwroblewski44 View Post
    Obviously '\0' is not the choice.
    Why not? That's how the end of a string is denoted in C normally
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by jwroblewski44 View Post
    thank you for the advice on multiplying by ten and adding the digit. Is there a specific char I can look for to notify the end of the string? Obviously '\0' is not the choice.
    Use the isdigit() function. If you don't know how this works, it's simply.
    Code:
    int isdigit(int ch)
    {
       if(ch >= '0' && ch <= '9')
         return 1;
       else
         return 0;
    
    }

    At some stage you'll need to decide what you do if the first non-digit character is not nul. But you can leave that for now. nul or '/0' or zero is a char value. It's only end of string by convention, and you can read and manipulate it like other char values.

  11. #11
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thank you sincerely to Malcolm McLean to your fast and helpful reponses. Here's my attempt at the following assignment..

    Code:
    /* detab- write a program 'detab' that replaces tabs in the input with
                the proper number of blanks to space to the next tab stop.
              Assume a fixed set of tab stops, say every n columns. Should
              n be a variable of a symbolic parameter? */
    #include <stdio.h>
    
    int stringtointeger(char **str);
    
    int main(int argc,char *argv[]){
        if(argc != 2)
            printf("Usage: %s n [tabstop every n columns]\n",argv[0]);
        else{
            int tabstop,k,c;
            if((tabstop = stringtointeger(argv)))
                if(tabstop > 10)
                    printf("Tab stop must be less than 10\n");
                else{
                    k = tabstop;       //holds the current tabstop locator
                    while((c = getchar()) != EOF){
                        if(c != '\t'){
                            printf("%c",c);
                            if(c == '\n')
                                k = tabstop;
                        }
                        else{
                            int i;for(i = k;i > 0;--i)
                                    printf(" ");
                        }
                        if(k > 0)
                            --k;
                        else if(k == 0)
                            k = tabstop;
                    }
                }
            else
                printf("Input Error\n");
        }
        return 0;
    }
    int stringtointeger(char **str){
        int i= 0,c,t = 0;
        
        if((c = str[1][i]) < '0' || c > '9')
            return 0;
        else{
            for(i = 0;((c = str[1][i]) != '\0' && c >= '0' && c <= '9');++i)
                t = (t * 10) + (c - '0');
            if(c != '\0')
                return 0;
            return t;
        }
    }
    How does that look?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of command line argument
    By natrajdreams in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 11:07 AM
  2. command line argument
    By csit in forum C Programming
    Replies: 4
    Last Post: 09-19-2007, 11:35 AM
  3. command argument line
    By Tonyukuk in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2003, 09:10 AM
  4. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM
  5. Command Line argument
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-12-2001, 03:24 AM

Tags for this Thread