Thread: command argument line

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    command argument line

    for example there are some functions about strings.for eample "sortstring()", How can I use command argument line with this function?

    Example: in dos command prompt you write

    s.exe byname

    then this program sorts strings by name. How can I do this?

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Your question ain't clear enough

    Hi,

    Unless one knows what kind of contents you are storing and what kind of sorts you want to perform, answering your question ain't gonna be easy. Still hope the below mentioned code helps you to start off...

    Code:
    #include <iostream>
    #include <cstring>
    
    int main(int argc, char *argv[])
    {
         if(!(strcmp(argv[1],"byname") )
         {
              // you would be sorting by name
         }
         else 
         {
              // sort it otherwise
         }
    
         return 0;
    }
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(!(strcmp(argv[1],"byname") )
    Dont forget to check that argc > 1, else the prog will crash and burn
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    than a lot but what is strcmp and why do we use "!"

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    and also I am talking about functions "sortbyname()" is a function. How can I use this function with command argument line.

  6. #6
    1. strcmp() is a function that compares two strings

    2. ! is used to mean not equal to 0 because this function returns 0 when the two strings aren't equal.

    3. As explained in the code:

    Code:
    #include <iostream>
    #include <cstring>
    
    int main(int argc, char *argv[])
    {
         if(!(strcmp(argv[1],"byname") )
         {
              sortbyname()// you would be sorting by name
         }
         else 
         {
              sortbysex()// sort it otherwise
         }
    
         return 0;
    }
    the argv[1] is the command line argument so if you were to say (using your first example) "s.exe byname" the program would execute the first if:

    Code:
    if(!(strcmp(argv[1],"byname") )
         {
              sortbyname()// you would be sorting by name
         }
    and would sort it by name. Undersatnd now?

    -Devouring One-
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM