Thread: Command Line Arguements

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Command Line Arguements

    I'm attempting to write some code that uses command line arguements to read x amount of character strings (words) from a file.

    I am unaware of how to open the file from the command line or whether I'd need another function specifically for opening the file and checking to make sure it's data is valid (strings between 1 and 15, file not able to be opened, or not enough data in the file.

    I know you use

    int main (int argc, char *argv[])

    How would I pass those to either an openFiles( ) function or just do all the checking in main?

    <<split from dead thread>>

  2. #2
    Logical Aesthetician codepoet's Avatar
    Join Date
    Nov 2006
    Posts
    14
    Code:
    void openFile ( char *filename );
    
    ...
    
    openFile ( argv[1] );
    Code:
    $question =~ /(bb|[^b]{2})/;

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by codepoet
    Code:
    void openFile ( char *filename );
    
    ...
    
    openFile ( argv[1] );
    Wow, thanks for the quick response.
    sorry I bumped the old thread, but I figured old better than new, guess not. Thanks for the pointer though.

    So now I can check the data arguements in the openFile function, correct?
    Last edited by Tha_Rappa; 11-26-2006 at 03:40 PM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    How do I check the 1 through 15 conditions and the amount of data in the file? Can I do that through the openFile function, or is that done through main?


    Example:
    my command line is $./a.out 6 data
    Where a.out is argv[0], 6 is argv[1] and data is argv[2]

    Would I check if argv[1] > 0 && argv[1] < 15 in main or in open files?
    Then I would have to pass argv[] into the openFile function.

    So I'd be left with openFile(char *filename, argv, *charArray) for my declarations? (I'd be sending out the charArray which I read from the file?

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    To open the file, you need fopen. Then you'll have to read it to and determine word for word if the words fit your designated criteria.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    may be this helps

    Code:
    #include<stdio.h>
    
    /* 
    argc in a tell how many no of arguments are there
    
    for example:
        
    a.out file.txt
    
    this would give you 2 as the result.
    
    and the argc is the one which actually gives you those values
    
    sow when your say argv[1]  it gives you file.txt
    */
    int main(int argc, char **argv)
    {
        FILE *fp;
        
        if(argc >= 2)
        {
            fp = fopen(argv[1],"r");
            
            if(fp != NULL)
                printf("File opened\n");
            else
                printf("Error: File cannot be opened\n");
        }
        else
        {
            printf("Error: Not enough arguments\n");
            printf("Press any ke to continue\n");
            getchar();
            return 1;
        }
        
        getchar();
        return 0;
    }
    ssharish2005

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thanks everybody, but my issue is that the user puts the amount of strings to find in the file on the command line.

    So an example of the command line would look like:
    $ ./a.out 9 file

    So far I have this: (but I know I have all kinds of issues still to resolve):
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    #define MAXWORD 15
    #define MAXLEN 20
    
    
    
    void printName (void);
    void sort(char strArray[][MAXLEN], int currentsize);
    int binSrch(char strArray[][MAXLEN], char *item, 
                int lowerbound, int upperbound);
    void printArray(char strArray[][MAXLEN], int currentsize);
    void openFile(char *pFPIn, int argc, char strArray[][MAXLEN]);
    int srch(char srtArray[][MAXLEN], int *item);
    int getValue(void);
    
    
    int main (int argc, char *argv[])
    {
     char strArray[MAXWORD][MAXLEN];
     int item, result; 
    
     printName(void);
     openFile(argv[1], argc, *strArray);
     sort (srtArray, currentsize);
     
     
     
    
     return 0;
    }
    
    
    void printName(void)
    {
     printf("\nName: Bryan Rapp\n");
    }
    
    
    void sort(char strArray[][MAXLEN], int currentsize)
    {
      int pass, trav;
      int min;
    
      for (pass = 0; pass < currentsize -1, pass++)
       {
        for (min = pass; trav = pass +1; trav < currentsize; trav++)
         if (strArray[trav] < strArray[min])
            min = trav;
         int temp = strArray[pass];
         strArray[pass] = strArray[min];
         strArray[min] = temp;
       } 
    
      printArray(strArray, currentsize);
    }
    
    
    int binSrch(char strArray[][MAXLEN], char *item,     
                int lowerbound, int upperbound)          
    {
      int result;
      int mid = (lb + ub) / 2;
    
      if (lb > ub)
       result = -1;
      else if (item == strArray[mid])
       result = mid;
      else if (item < strArray[mid])
       result = binSrch(strArray, *item, lowerbound, mid -1);
      else
       result = binSrch(strArray, *item, mid +1, upperbound);
    
      return result;
    } 
    
    int srch(char srtArray[][MAXLEN], int *item)
    {
      return binSrch(srtArray, *item, 0, currentsize - 1);
    }
    
    
    void printArray(char strArray[][MAXLEN], int currentsize)
    {
     int i;
     
      printf("\n%15s%20s", "Item", "Word");
      printf("\n%15s%20s", "----", "----");
    
     for(i = 0; i > currentsize; i++)
      {
      // printf("\n%5s%20s", "Item", "String");
      // printf("\n%5s%20s", "----", "------"); 
         printf("\n%15d%20s", i, strArray[i]);
    
      }
    
    void openFile(char *pFPIn, int argc, char strArray[][MAXLEN])
    {
     if (argc < 0)
       printf("\nNumber %d is not between 1 and 15.", argc);
      else if (argc > 15)
       printf("\nNumber %d is not between 1 and 15.", argc);
    
     if((*pFPIN = fopen(argv[2], "r")) !=NULL)
      {
       
    
      }
    
    
    }
    
    char getValue(void)
    {
     char item;
     printf("\nPlease enter a word ("q" to quit): ");
     scanf("%s", &item);
     
     return item;
    }
    Last edited by Tha_Rappa; 11-26-2006 at 07:51 PM.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you missed return 0 in your main

    ssharish2005

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by ssharish2005
    you missed return 0 in your main

    ssharish2005
    Yea, I havent gotten that far yet. I'm still working on the functions and the passing.
    Anybody have any advice for how to fix the openFile function?
    It needs to tell if main has enough arguements (or maybe this is in main?), and it also needs to check to make sure that argc is between 1 and 15 and that the data file has enough data to process.
    Last edited by Tha_Rappa; 11-26-2006 at 06:41 PM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If I understand what you want to do:
    Code:
    linecount = 1
    lineswanted = argv[1]
    maxlines = 15
    
    if lineswanted > maxlines
       screwed up
    
    while  linecount < argv[1] 
       read a line
       if that worked
          parse it for words
          linecount++
       else 
          break
    endwhile
    
    if linecount != lineswanted
       we broke early, return an error
    else 
       return success
    I'm sure that would work.

    The other issue is that you're confused about what argc means. argc is how many parameters where passed to main, so that we can subscript argv[] properly. It's not the sample value here:

    $ /a.out 9 file

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Well I'm confused because of the examples I was shown.
    In the examples argc was the amount of words read out of a specific file.
    Argc (in this case) is to between 1 and 15. And argv[] is supposed to be where the data (words) come from.
    Basically this program is supposed read argc amount of character strings out of a file, sort them by ASCII value, and then let the user search for a specific word in the array filled from the file.

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    argc gives you the number, as in the amount, of command line arguments. argv[] is an array of strings for which each entry points to one of those command line arguments.

    AKA

    argv[0] will be the name of your program, argv[1] will be the argument after that, and so on...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    So argv[1] would be equal to the number that was on the command line? So in the example of

    ./a.out 9 file

    argv[1] would equal 9
    and
    argv[2] would be the filename?

    I'm guessing fully understanding this would help me write it....

    Ok, so if I'm in main can I check to make sure that I have enough command line arguments?

    This is what I'm trying to write, and I don't know the best way to get there:
    The command line will have the executable file name (usually "a.out") as
    well as two arguments:
    1) an integer which is the number of strings to read from the file
    2) an input filename
    $ ./a.out 5 infile

    There are several potential errors:
    1) not enough command line arguments
    2) integer not between 1 and 15
    3) infile not able to be opened
    4) not enough words in the file

    Part I: Read the specified number of strings into a two-dimensional char
    array (what may be considered a 1D array of strings). The maximum number of
    strings is 15 and the maximum length of each is 20 (not including null).
    It is perfectly acceptable if there are more than the specified number of
    words in the file. Then sort ONLY the segment of the array which is being
    used into ascending ASCII order. You must use a selection sort.
    So I guess what' I'm wondering is how would this work in the openFile ( ) function? Or do I need to create another function for the checking of the command line arguments?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > So argv[1] would be equal to the number that was on the command line?
    Yes.

    Read your console from left to right:
    $ /a.out 9 <file>
    Each word is a parameter, starting at argv[0].

    You seem confused about how to approach this. Lets go over the error conditions then.

    There are several potential errors:
    1) not enough command line arguments
    So there are three in total okay? That means that argc should equal three.
    2) integer not between 1 and 15
    Thus, if wordcount = 1 when you start, wordcount will equal 15 when you finish. It's probably a loop condition.
    3) infile not able to be opened
    I think you have a handle on this part, you just need to figure out how to parse words from a line in the file.
    4) not enough words in the file
    Well, you read as much as you can. When you've read enough words or reach the end of the file (whatever happens first) check to make sure that wordcount == wordswanted. If it's not, you have an error to report and need to do something, like end the program.

    Btw, you might have to break up openFile into more functions, because it can be a rather long algorithm to write. Your decision.
    Last edited by whiteflags; 11-26-2006 at 08:18 PM.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    So should I check argc in main?

    Like:
    Code:
    if (argc != 3)
     printf("Not enough command line arguments");
    else if (argv[1] < 1 || argv[1] >15)
     printf("Number %d is not between 1 and 15.", argv[2]);
    now how to check conditions 3 and 4, granted the above is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguements in functions.
    By SlyMaelstrom in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2005, 07:41 AM
  2. Comparing command line arguements to a string.
    By SlyMaelstrom in forum C++ Programming
    Replies: 14
    Last Post: 10-30-2005, 04:56 AM
  3. command line arguements
    By screamer903 in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 07:59 AM
  4. Arguements for gnome?
    By mart_man00 in forum Tech Board
    Replies: 12
    Last Post: 07-30-2003, 04:46 PM
  5. Command Line Arguements
    By scaven in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 05:36 PM