Re: cpmmand line arguments



neha_chhatre@xxxxxxxxxxx wrote:

hello

tell me how to compile as well as run command line arguments.

You cannot compile or run command line arguments. What you can do is
pass command line arguments to programs designed to accept them. In C
you do this by defining main as follows:

int main(int argc, char *argv[]) { /* ... */ }

Obviously argc and argv can be any legal identifier but these two names
are practically universal for this purpose. Also argv could be written
as char **argv.

The general method is to first test argc. It could be zero or any
positive value. If it is non zero, then argv[0] to argv[argc-1] contain
the arguments, whatever they are. If argc is non-zero then argv[0] is
the program's invocation name, which may not be the same as the
executable file name of the program. argv[argc] is a null pointer.

Also let me know how to open a particular text file using command line
asgument

One simple example is:

int main(int argc, char **argv) {
FILE *fp;

if (argc > 1) {
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "File: %s: open failed.\n", argv[1]);
exit(EXIT_FAILURE);
}
}
else {
fprintf(stderr, "Usage: program filename\n");
exit(EXIT_FAILURE);
}
/* ... */
}

say for example
if there are three text file pt1,pt2,pt3 and if i want to open pt1
please let me know as soon as possible

Adapt from the above method.

PS. You have a habit of starting sentences as "tell me ..." etc., which
may strike some people as rude or arrogant. You might want to be a bit
more polite and do a bit more of your own homework. At least posting
whatever you have attempted so far is better than demanding answers.

.



Relevant Pages

  • Re: What does "*" mean in "int main(int argc, char *argv[])"?
    ... argv- is the array of parameters passed in the command line. ... it is a pointer to an array of type char. ... You can use argc in a for loop to process the argv[] values. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: CLIL source code from book "Firmware demystified"
    ... > I don't quite understand WHY I can pass 'argc' and 'argv' to another ... The reason I ask this is every command I type in command line is ... In particular, what do you mean by "single-running processes, not as ...
    (comp.arch.embedded)
  • Re: Command line arguments without argc and argv
    ... > Is it somehow possible to retrieve the command line arguments of a program ... > without using argc and argv? ...
    (comp.os.linux.development.system)
  • Re: function arguments question (newbie)
    ... > The following useless code seems to compile and run, ... > My question is concerning the style of declaring the argument types ... > Of course I'm not asking specifically about 'argc' and 'argv', ...
    (comp.lang.c)
  • Re: Command line arguments
    ... The following program prints all given command line arguments: ... argc is guaranteed to be non-negative ... argv is an array of pointers to strings holding ... argv[argc] is guaranteed to equal NULL ...
    (comp.lang.c)