What are argc and argv[] used for?

Also how do I make a program accept only a string like 'count this

Page 1 of 1

4 Replies - 9591 Views - Last Post: 09 March 2008 - 10:22 PM Rate Topic: -----

#1 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

What are argc and argv[] used for?

Posted 09 March 2008 - 06:19 PM

A lot in C++ I see in the main parameters the (int argc, char* argv[]). What is that used for?

Also, unrelated but how do I make a program accept something like 'count thisfile.txt' or accept 2 values like 'unscramble scrambled.txt unscrambled.txt' so it will unscrambled the words in scrambled.txt and write them to unscrambled.txt.
Is This A Good Question/Topic? 0
  • +

Replies To: What are argc and argv[] used for?

#2 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: What are argc and argv[] used for?

Posted 09 March 2008 - 06:35 PM

They're used to pass command line arguments to your program.

'argc' is the number of arguments, and 'argv' is a pointer to the arguments.

A command line argument would look like ./example program -argument1 -argument2, or example.exe argument1 argument2. Basically anything that comes after the executable is an argument.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5348
  • View blog
  • Posts: 27,305
  • Joined: 10-May 07

Re: What are argc and argv[] used for?

Posted 09 March 2008 - 06:38 PM

View Postdevilsson2010, on 9 Mar, 2008 - 06:19 PM, said:

A lot in C++ I see in the main parameters the (int argc, char* argv[]). What is that used for?


http://www.dreaminco...wtopic30387.htm
Was This Post Helpful? 0
  • +
  • -

#4 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Re: What are argc and argv[] used for?

Posted 09 March 2008 - 09:30 PM

Oh, thank you I get it now, what was misleading me was that I didn't know where to put the arguments in. But I have a crappy test program and it's not working, I can't post the code because it's on my other pc and its internet isn't working. However, what I'm doing is going into the command prompt then typing cd c:\whatever directory its in. Then typing: testing.exe count file.txt . And I keep getting the stupid message that says your input should be in the form of 'count filename.txt'. (That's the error the program gives when it doesn't work). Am I doing anything wrong?

I think it's one part of the program that says:
if (argc != 2)
{cout << "Input should be in the form of 'count filename.txt'" << endl;}

This post has been edited by devilsson2010: 09 March 2008 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: What are argc and argv[] used for?

Posted 09 March 2008 - 10:22 PM

it would be better if you could post the code as I don't understand what you mean
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1