Command-line Parameters in C program?

How do I read command-line parameters in C? For example, in

./test --help

or

./test --build

how do I access "--build" or "--help"?

-------------Problems Reply------------

Your parameters are in argv:

int main(int argc, char **argv)

if you printf the content of argv (argv[0],argv[1] etc) youll get the idea.

try:

int main (int argc, char **argv)
{
for(int i = 0;i< argc;i++)
printf("%s\r\n",argv[i]);
}

You can use the argc and argv arguments to the main function and do different things based on them:

#include <string.h>
void dohelp(void) { /* whatever; */ }
void dobuild(void) { /* whatever; */ }
int main(int argc, char **argv) {
if (argc == 2) {
if (!strcmp(argv[1], "--help")) dohelp();
if (!strcmp(argv[1], "--build")) dobuild();
}
return 0;
}

argc contains the number of parameters passed by the shell to your program, including the program name. So myapp --help gets an argc of 2.

argv are the arguments themselves. The last argv (argv[argc]) is the NULL pointer.



Edit: the parameters don't need to be named argc and argv, but naming else something else is very, very bad!

int main(int foo, char **bar) /* RGAGGGGHH */
int main(int n, char **options) /* RGAGGGGHH */

The very basic is to use the arguments (int argc, char *argv[]) and you can parse those directly.

One more advanced method is to use getopt... http://www.gnu.org/s/libc/manual/html_node/Getopt.html

There are several ways to do it [as usual]. Command line arguments are read from argv (passed to main along with argc).

You can parse those yourself and have a bit switch setting flags each time a new option is found in argv. Or you can use a library to parser command line arguments. I suggest libc getopt (google it).

Category:c# Views:0 Time:2011-03-12
Tags: c# parameters gcc

Related post

  • Limit of passing in files as command line parameters 2012-03-16

    I'm trying to pass through files as command line parameters into my program (it does a bunch of processing of the file name and the type of file). When I pass through more than 19 files (they are just drag and dropped on to the application) I get the

  • Can .NET check other running programs command line parameters? 2008-09-04

    We've got an interesting case where we are trying to determine how different instances of our app were launched. Is there any way for .NET to be able to query another running instance and get the command line parameters passed to that instance? I've

  • How do I recognize command-line parameters in my Delphi program? 2009-10-20

    I plan to run specific application commands every X days using Task Scheduler. Do I have to write support for command-line parameters first, so Scheduler can execute it? If so, does anyone know any good command-parameter components? --------------Sol

  • Modifying text files and executing programs with command line parameters in c# or c++ on Linux 2010-04-10

    I have a need to create a utility in Suze Linux. The utility will make modifications to some text files, and then use the information in those text files to program a device in the computer using a different executable which accepts command line para

  • I installed Explorer 8 and when I try to run it my anti virus states: Attempt to run browser with command line parameters: "C:\Program Files\Internet Explorer\iexplore.exe" SCODEF:2784 CREDAT:79873. 2013-04-07

    Is this a virus or normal to run with these parameters? --------------Solutions------------- It's "by design" in IE8. Not a virus. See: http://blogs.msdn.com/askie/archive/2009/03/20/how-to-i-determine-which-ie-tabs-go-to-which-iexplore-exe-process-w

  • How to run a console application with command line parameters in Visual C++ 6.0? 2008-11-09

    I've got a console application that compiles and executes fine with Visual C++ 6.0, except that it will then only get as far as telling me about missing command line parameters. There doesn't seem to be anywhere obvious to enter these. How do I run o

  • Split string containing command-line parameters into string[] in C# 2008-11-18

    I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the comman

  • Passing JUnit command line parameters in eclipse 2009-03-25

    I have recently been using junit in eclipse and I am still learning. I know how to pass command line parameters in eclipse, but how do I pass them to a test case in Junit? Also how do I access them? --------------Solutions------------- You cannot pas

  • How do I get the command-line parameters for certain button clicks in a application? 2009-10-15

    I want to start a program using Delphi code, and "command" it to perform an action, in this case a button click. I know you can start a program using the command line, but I would need the right paramaters for the button click. How or where can I fin

  • Command Line Parameters 2009-11-17

    I'm trying to add my file in Visual Studio as command line parameters. I know my code works since when I use fopen("whole path here", "r"), it runs. I then add the file as a command line parameter instead, and I get no such file or directory. Any tho

  • How can I read command line parameters from an R script? 2010-01-27

    I've got a R script for which I'd like to be able to supply several command-line parameters (rather than hardcode parameter values in the code itself). The script runs on Windows. I can't find info on how to read parameters supplied on the command-li

  • .net command line parameters? 2010-02-07

    I have vb app that calls another vb app using Process.Start(PROGRAM). My question is how can I pass a parameter to the PROGRAM and how can I intercept it in the PROGRAM? --------------Solutions------------- You can add a second string with the comman

  • Scala: Best way to parse command-line parameters (CLI)? 2010-02-23

    What's the best way to parse command-line parameters in Scala? I personally prefer something lightweight that does not require external jar. Related: Java library for parsing command-line parameters? What parameter parser libraries are there for C++?

  • Eclipse: Debug script that expects command line parameters 2010-08-05

    I have a python script I am trying to debug in eclipse. I can execute it, breakpoint all that jazz, but this specific script requires a handful of command line parameters. Is it possible to setup my dev environment in eclipse to put these parameters

  • Custom Command Line Parameters 2011-01-24

    I have a problem with command line parameters. I finished the program so I can start it like this from command line: program.exe test.txt copy_test.txt Basically, my program does the following : inputs some text file sorts it and copy to a new text f

  • Obtaining command line parameters 2011-05-12

    What are the ways in which I could make an executable to expose a function's parameters as command line parameters. I know of using int main( int argc, char **argv ) Are there alternatives ? Is it possible to invoke this program from a script languag

  • Command line parameters, standard aproach to parse? 2011-10-26

    I've been reading a bit about how programs handle command line parameters. But the information seems to be "incomplete", things I've read: Options may have a preceding '-' or '/' sign if front of them. Options can have additional arguments (which go

  • groking command line parameters in a python script 2011-12-20

    I am relatively new to python. I want to write a script and pass it parameters like this: myscript.py --arg1=hello --arg2=world In the script, I want to access the arguments arg1 and arg2. Can anyone explains how to access command line parameters in

  • Preventing multiple instances - but also handle the command line parameters? 2011-12-31

    I am handling from my Application associated extension files from Windows. So when you double click a file from Windows it will execute my program, and I handle the file from there, something like: procedure TMainForm.FormCreate(Sender: TObject); var

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.151 (s). 11 q(s)