cpmmand line arguments

N

neha_chhatre

hello

tell me how to compile as well as run command line arguments.Also let
me know how to open a particular text file using command line asgument

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
 
R

Richard Heathfield

(e-mail address removed) said:
hello

tell me how to compile as well as run command line arguments.Also let
me know how to open a particular text file using command line asgument

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

This information is readily available from any good C tutorial book or
reference book. See, for example, "The C Programming Language", 2nd
edition, by Kernighan and Ritchie, or "C How to Program" (any edition), by
Deitel and Deitel, or "C: A Reference Manual" (any edition), by Harbison
and Steele. Look up "command-line arguments" in the index.
 
S

santosh

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.
 
D

dj3vande

Richard Heathfield said:
See, for example, "The C Programming Language", 2nd
edition, by Kernighan and Ritchie, or "C How to Program" (any edition), by
Deitel and Deitel, or "C: A Reference Manual" (any edition), by Harbison
and Steele.

Without having a copy to check, I'm pretty sure the first edition of
K&R will also answer the OP's question.
(Though there are of course other Very Good Reasons to prefer the
second.)


dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top