Command Line Arguments???

This is a discussion on Command Line Arguments??? within the C++ Programming forums, part of the General Programming Boards category; I was reading the command line argument tutorial and I realized that I didn't know what command line arguments are. ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Question Command Line Arguments???

    I was reading the command line argument tutorial and I realized that I didn't know what command line arguments are. Can someone quickly explain that to me. And if possible, post an example so I can more easily understand it.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    If you run your program from a shell (in your case I am guessing a dos prompt is the easyest way to go) the command line arguments are what comes are the programms name followed by whatever you typed after its name
    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main(int argc, char *argv[]) {
      if(argc==2) {
        ifstream in(argv[1]);
        char ch;
        int lines=0;
        while(in.get(ch)) if(ch == '\n') lines++;
        cout << argv[1] << " has " << lines << " lines" << endl;
      }
      return 0;
    }
    Compile this normally and name the resulting program lc.exe. Open a dos prompt and go to the directory where lc was created. type lc afile.txt and assuming afile.txt is a file in the current directory this will count the lines in that file. Running from the command line also lets you see the output without having windows close the window too quickly.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Take a basic DOS command: copy.
    Code:
    copy C:\x.exe C:\Folder\
    'C:\x.exe' and 'C:\Folder\' are command line arguments. Its just another way of passing values to a program.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    using namespace std;?

    Can you please explain the purpose of

    Code:
    using namespace std;
    Thanks!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: using namespace std;?

    Originally posted by luckygold6
    Can you please explain the purpose of

    Code:
    using namespace std;
    Thanks!
    Its for the new standard headers. In the old <iostream.h> everything was in the global namespace. In the new header <iostream> its in the standard namespace. So you must either do

    Code:
    #include <iostream>
    
    int main( void )
    {
      std::cout << "Hi" << std::endl;
      return 0;
    }
    Or...
    Code:
    #include <iostream>
    using namespace std;
    
    int main( void )
    {
      cout << "Hi" << endl;
      return 0;
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @luckygold6: Have a read through the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Hum... that'd be a good idea...

    Thanks!

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    I tried to replace

    Code:
    while(in.get(ch))
    with

    Code:
    while (in >> ch)
    but the program is no longer counting the correct number of lines. Can you explain why?

    Thanks!

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Nevermind, I figured it out.

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, 02:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 07:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 01:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 11:48 AM

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