Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,048 people online right now. Registration is fast and FREE... Join Now!




How to pass parameters to main() and use them?

 
Reply to this topicStart new topic

How to pass parameters to main() and use them?

crammer008
post 28 Mar, 2006 - 02:38 AM
Post #1


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Dear all,

I have the following code:

CODE

int main(int argc, char *argv[])
{
   int i;
   for (i=0;i<=argc;i++)
   {
     cout<<"argv["<<i<<"]: "<<argv[i]<<endl;
   
   }
 return 0;
}


Assume the code is compiled to be "openfile.exe".
If I type "openfile data.txt" in DOS mode, obviously the string "data.txt" will be passed to main() function.
My question is: How to save the content of the argv[1] to a string variable? Although I can use "cout" to display the content of argv[] on screen I don't know how to transfer its content to another variable because I want to use the content to do some operation in main() function.

Looking forward to your guidance!
Thanks a lot!
Crammer008

This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:19 AM
User is offlineProfile CardPM

Go to the top of the page

sontek
post 28 Mar, 2006 - 02:48 AM
Post #2


D.I.C Regular

Group Icon
Joined: 13 Sep, 2001
Posts: 283



Thanked 1 times

Dream Kudos: 85
My Contributions


I'm not that familiar with C++ but I think you could do something like

CODE

ofstream stream;
stream.open (argv[i]);
stream >> stringVariable
User is offlineProfile CardPM

Go to the top of the page

crammer008
post 28 Mar, 2006 - 02:53 AM
Post #3


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


The problem is I have to type the command line like the below:

CODE

openfile mytxt


So I have to do the following operation in main() function:

CODE

"mytxt"+".txt" = mytxt.txt


So using string variable to operate it is necessary.
How to do it?

Thanks a lot!
Crammer008

This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:20 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2006 - 06:50 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Do you need an actual string object, or a c style string of characters?
CODE

#include<iostream>
#include<string>
using namespace std;

int main(int argc, char *argv[])
{
  string arg1 = argv[1];
  //char *arg1 = argv[1];
  cout<<arg1<<endl;
  return 0;
}

Uncomment the char line and comment the string line, depending on what suits your needs.

Alternately, you can declare a vector of strings, and push each argument onto the pile, then pop as required.
User is offlineProfile CardPM

Go to the top of the page

crammer008
post 28 Mar, 2006 - 08:41 AM
Post #5


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


thank you very much:)

Your code give me great help:)

Crammer008
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:44AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month