cpmmand line arguments

Discussion in 'C Programming' started by neha_chhatre@yahoo.co.in, Feb 24, 2008.

  1. Guest

    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
    , Feb 24, 2008
    #1
    1. Advertising

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

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 24, 2008
    #2
    1. Advertising

  3. santosh Guest

    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.
    santosh, Feb 24, 2008
    #3
  4. Guest

    In article <>,
    Richard Heathfield <> wrote:

    > 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

    --
    Dave Vandervies dj3vande at eskimo dot com

    Nothing is perfect. Not even Windows sucks perfectly.
    -- Jay Maynard in the Scary Devil Monastery
    , Feb 24, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,299
    Matt Humphrey
    Oct 18, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    4,935
    Josiah Carlson
    Apr 6, 2004
  3. kaushikshome
    Replies:
    4
    Views:
    759
    kaushikshome
    Sep 10, 2006
  4. scad
    Replies:
    23
    Views:
    1,158
    Alf P. Steinbach
    May 17, 2009
  5. Marek Stepanek
    Replies:
    12
    Views:
    410
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page