c++ tutorial 14 - argc and argv

This is a discussion on c++ tutorial 14 - argc and argv within the C++ Programming forums, part of the General Programming Boards category; The code I was looking at was here Command Line Arguments in C++ - Cprogramming.com (copied and pasted to the ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    c++ tutorial 14 - argc and argv

    The code I was looking at was here
    Command Line Arguments in C++ - Cprogramming.com
    (copied and pasted to the bottom of this post for your viewing pleasure)

    I pretty much wrote it out just replacing agrv[1] with a file i created (creatively called file.txt)
    however, I couldn't get it to work. i told it to print out argc and it outputted 1, rather than 2.
    when i changed the first if to give it the logic to continue as though argc was ==1 eg
    Code:
    if (argc>2)
    it then works fine.

    i don't get why since tutorial this tells me it should be 2!

    any help much appreciated.
    tyia

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main ( int argc, char *argv[] )
    {
      if ( argc != 2 ) // argc should be 2 for correct execution
        // We print argv[0] assuming it is the program name
        cout<<"usage: "<< argv[0] <<" <filename>\n";
      else {
        // We assume argv[1] is a filename to open
        ifstream the_file ( argv[1] );
        // Always check to see if file opening succeeded
        if ( !the_file.is_open() )
          cout<<"Could not open file\n";
        else {
          char x;
          // the_file.get ( x ) returns false if the end of the file
          //  is reached or an error occurs
          while ( the_file.get ( x ) )
            cout<< x;
        }
        // the_file is closed implicitly here
      }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Perhaps you did not actually provide a command line argument?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    You're supposed to type in something like
    myprog.exe file.txt

    Or if you're using an IDE to compile and run programs, there should be a dialog somewhere where you can specify "command line parameters".
    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.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    thanks for the rapid replies although i don't fully understand them!

    i think the command line argument would be the myprog.exe in the second reply?

    when i have the wrong value for argc, the error message from the code appears with

    Code:
    usage: C:\Users\.....\argc and argv tutorial
    i assumed that the "argc and argv tutorial.exe" is what i should put in where argv[0] lies in the main body, but this still outputs argc = 1 so i still don't understand this concept.

    and i'm using a compiler (codeblocks) although i don't understand why specifying command line parameters can/would be done from somewhere else?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by thankyoukindly
    i think the command line argument would be the myprog.exe in the second reply?
    No, that is the program name. The command line argument is file.txt

    Quote Originally Posted by thankyoukindly
    and i'm using a compiler (codeblocks) although i don't understand why specifying command line parameters can/would be done from somewhere else?
    CodeBlocks is not a compiler; CodeBlocks is an IDE. When you run your program from within CodeBlocks, no command line arguments are supplied unless you go to Project -> Set program's arguments to set them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    thanks for that info, very clear! it's much appreciated.

    and that sorted it.

    is it correct to say without adding any manually through the ide, argc will always be 1?
    lastly, is there anywhere you know of that explains the argc/argv basic concepts as i couldn't find anything via google or other tutorials (and my book on c++ is in the post)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by thankyoukindly
    is it correct to say without adding any manually through the ide, argc will always be 1?
    Yes. Well, it might be 0, but I think you don't have to worry about that.

    Quote Originally Posted by thankyoukindly
    lastly, is there anywhere you know of that explains the argc/argv basic concepts as i couldn't find anything via google or other tutorials (and my book on c++ is in the post)
    You found the tutorial.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv and argc
    By antros48 in forum C Programming
    Replies: 19
    Last Post: 09-30-2011, 09:26 AM
  2. how to use argc and argv
    By Salahuddin in forum C Programming
    Replies: 19
    Last Post: 09-09-2011, 04:53 AM
  3. argc, **argv in C++.
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2007, 12:09 AM
  4. argc and argv
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 06:21 PM
  5. Argv And Argc
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 03:43 PM

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