473,385 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

is it possible to pass string argv[] in main?

Hello!

I want to use following statement to pass command-line arguments into
main function:

int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}

return 0;
}

and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???

Aug 6 '07 #1
5 13524
Pawel_Iks wrote:
I want to use following statement to pass command-line arguments into
main function:

int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}

return 0;
}

and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???
Construct a vector of strings:

if (argc 0) {
std::vector<std::stringsargv(argv, argv + argc);
// do something with 'sargv'
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 6 '07 #2
On Aug 6, 1:43 pm, Pawel_Iks <pawel.labed...@gmail.comwrote:
Hello!

I want to use following statement to pass command-line arguments into
main function:

int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}

return 0;

}

and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???
Yes, you have to use char**.
You can convert it like so:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>

int main( int argc, char* argv[] )
{
std::vector< std::string args;

for( int i = 0; i != argc; ++i )
args.push_back( std::string( argv[i] ));

std::copy( args.begin(), args.end()
, std::ostream_iterator<std::string>( std::cout, "\n" ));
}

Aug 6 '07 #3
On Aug 6, 10:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Pawel_Iks wrote:
I want to use following statement to pass command-line arguments into
main function:
int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}
return 0;
}
and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???
Construct a vector of strings:
if (argc 0) {
std::vector<std::stringsargv(argv, argv + argc);
// do something with 'sargv'
}
Just for the record, you don't need the if; argc is guaranteed
to be >= 1. (If the name of the program isn't available---never
the case under Windows or Unix---, then argv[0] is the empty
string.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 7 '07 #4
On Aug 7, 12:40 pm, James Kanze <james.ka...@gmail.comwrote:
On Aug 6, 10:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


Pawel_Iks wrote:
I want to use following statement to pass command-line arguments into
main function:
int main(int argc, string argv[])
{
if (argc>0)
{
//do something
}
return 0;
}
and it's an error ... do i have to use *char type, and if yes, how to
convert it into string type which is more diserable for me ... any
clues, solutions ???
Construct a vector of strings:
if (argc 0) {
std::vector<std::stringsargv(argv, argv + argc);
// do something with 'sargv'
}

Just for the record, you don't need the if; argc is guaranteed
to be >= 1. (If the name of the program isn't available---never
the case under Windows or Unix---, then argv[0] is the empty
string.)

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -

- Show quoted text -
are you learning in niit?

Aug 7 '07 #5
in*****@gmail.com wrote:
int main( int argc, char* argv[] )
{
std::vector< std::string args;

for( int i = 0; i != argc; ++i )
args.push_back( std::string( argv[i] ));
Why do it like that when you can do it more easily like:
int main( int argc, char* argv[] )
{
std::vector< std::string args(argv, argv+argc);
Aug 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: qazmlp | last post by:
void func() { // Is it by anyway possible to read the value of the first command line parameter i.e. argv here ? } int main() { func() ; // No command line arguments are passed to func(). }
4
by: darin dimitrov | last post by:
Hello, I need help with an algoritm that given a set of "n" distinct numbers will generate all the possible permutations of fixed length "m" of these numbers WITH repetitions (a total of n^m...
2
by: Tomás | last post by:
Up until lately I've been writing all mad kinds of code for accomplishing things, but lately I've decided to lean more toward the whole readability, etc. side of things. I have a command line...
4
by: Vusi | last post by:
#include <err.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <net/route.h> #include <net/if.h> #include...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
8
by: key9 | last post by:
Hi All I defined a class "RootElement" And want the class can be inherit tree's root. The main idea is using std::string as the "config" of the instance the module pass the compile with the...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
5
dlite922
by: dlite922 | last post by:
All i want to do in C++ is pass a few arguments, concatenate them into variable and call system() I've gotten this far: #include <stdio.h> #include <stdlib.h> #include <string>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.