LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2008, 01:50 PM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
C++ progamme with options


Is it possible to execute a c++ programme in the terminal, say prog, which normally asks several input values, with an option tag, say "-d" to make it run the programme with a given set of default values? So, instead of running
Code:
prog
in the terminal and then giving certain values that are necessary for its completion, I would sometimes like to use
Code:
prog -d
, where prog is executed but with predefined "default" values. Is this possible and if so, how do I do that? Without "-d" I would like the programme to be asking for input.
 
Old 02-18-2008, 02:05 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Well a c++ program is a program written in C++, if I got your idea right. That means it does whatever you tell it to; you can easily write it in such a manner that it checks the arguments given, and if there is no -d argument, then skips the questions and uses default values (maybe by setting a flag which is checked later in the program, or something), or if the argument is given, then does ask the questions.

You'll write the program depending on what the program needs to do. Use argc and argv, which are given to main function as arguments, to determine the number of arguments given and the arguments themselves, and then deal with them the way you like.


EDIT: well here's a short example to demonstrate how it starts. It shows how to access the arguments; after this you'll want to test them and do whatever you want.

Code:
#include <iostream>

// Function main takes two arguments: argc, the number of arguments
// when the program is executed, which is one or more, and argv
// which contains those arguments in argv[0]...argv[argc-1]

int main(int argc, char **argv)
{
        // Print out the number of arguments; note that if you
        // start the program without any arguments except for
        // the executable name, argc is 1 and not 0.

        std::cout << "Number of arguments given: " << argc << std::endl;

        int i=0;            // index number for the loop
        char Argument[10];  // char array of 10 elements to store
                            // the arguments one at a time

        while (i < argc)    // loop trough all the arguments
        {
                // copy the argument at hand in each round
                // to Argument[], then print it and increase index

                strcpy(Argument, argv[i]);
                std::cout << "Arg " << i << " is " << Argument << std::endl;
                i++;
        }

        return 0;     // ...and return 0 to say we're clean
}
Sample outputs look like this:
1) without extra arguments
Quote:
$ ./a.out
Number of arguments given: 1
Arg 0 is ./a.out
2) with some arguments
Quote:
$ ./a.out A BB CCC
Number of arguments given: 4
Arg 0 is ./a.out
Arg 1 is A
Arg 2 is BB
Arg 3 is CCC

Last edited by b0uncer; 02-18-2008 at 02:25 PM.
 
Old 02-18-2008, 02:27 PM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
You can do that, or you can look into <getopt.h> especially getopt_long. I know there is a great chapter in Advanced Linux Programming called "Writing good GNU/Linux Software" and it describes the way to parse options.
http://www.advancedlinuxprogramming.com/downloads.html
 
Old 02-18-2008, 02:30 PM   #4
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Sure there is, but it's good to know the basics before building a space rocket, even if you had a quick manual.
 
Old 02-18-2008, 03:11 PM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Agreed. Just felt like recommending that book again. It's great
 
Old 02-19-2008, 04:31 AM   #6
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
Thanks a lot, that's exactly what I wanted! You guys rock!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
display options autophil Linux - Hardware 5 07-24-2007 02:45 PM
What are my other options? JiggaJerry Fedora 15 03-21-2005 03:53 PM
Kernel 2.6.2 options question - LOCKED options ? tvojvodi Linux - General 0 02-17-2004 04:23 AM
options? Bullzeye Debian 1 08-27-2003 05:14 AM
Help with Boot Options DamianY2J Linux - Newbie 21 07-25-2003 12:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration