Thread: Command line arguments

  1. #1
    Registered User
    Join Date
    Nov 2006

    Command line arguments

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    main(int argc, char *argv[] )
       cout << "argc is: " << argc << endl;
       if (argc >= 1)
           cout << "This program was run as: " << argv[0] << endl;
       if (argc > 1)
           cout << "This program received the following arguments:" << endl;
           for (int i = 1; i < argc; i++)
              cout << "\t" << argv[i] << endl;
    	return 0;
    Create a program that reads from the file specified by a command line argument, then writes out the contents reversed line-by-line to a separate file specified on the command line. For example, the user enters on the the commandline: reverse -i input.txt -o myoutput.txt. Your program must search the command line argument list given by the main program parameters, argc and argv. The commandline arguments may be specified in any order, though the file names will always be found immediately adjacent to the -i or -o switch.

    Your program must:

    1.Search the command line arguments for input and output file names.
    2.Open the input and output files, displaying an appropriate error message on open file errors
    3.Read the input file line-by-line into a 256-character C-string buffer using fgets() or the iostream getline() member function, then write out the line to the output file.
    4.Close both files and end the program
    The above code is what I've done so far. However, I don't know how to run this on command prompt. Please help. I'd appreciate it.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Other than posting your assignment, do you have a question?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    In windows:
    Start/Run: cmd
    c: //or print your drive where the exe is located to switch to it (if drive is the same as the default drive it is optional
    cd <your exe folder>
    <yuor app>.exe 1 2 "The 3rd argument" 4 "5"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > However, I don't know how to run this on command prompt.
    In the IDE, goto project settings.

    Where it talks about debugging or running the program, you can set the initial set of command line arguments to send to the program being debugged.

    Here, you would type in the parameters exactly like you would do typing direct to the command line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM