Command line arguments

This is a discussion on Command line arguments within the C++ Programming forums, part of the General Programming Boards category; #include <cstdlib> #include <iostream> using namespace std; Code: int main(int argc, char *argv[] ) { cout << "argc is: " ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Command line arguments

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    Code:
    int
    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;
       }
    
       cin.get();
    
    	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
    Posts
    5,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21