By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

C++ arguments

P: 1
Hi, I'm a (very) new programmer to C++

I was trying to make a simple (small as possible!) program that would run the arguments as a command line with system()

For example:

starter.exe notepad.exe c:\boot.ini

would preform the same action as:
Expand|Select|Wrap|Line Numbers
  1. system("start notepad.exe c:\boot.ini");

However I can't seem to figure out how to get the 2d array given to main() into one big array (one entry after another - think argv[n] + argv[n+1] + arg[n+2] and so on. The most I can do is get the first "argument" (in reality I want EVERYTHING after my .exe to be one argument) to system().

system() wants a char array, so strings are out (as far as I understand - very little). It also most definatly doesn't want an array of char arrays.

Really I want to take each element of argv[] and put them into a 1-dimensional char array one after the other (inserting the spaces that get removed when the OS does it's voodoo to the arguments), call it argx, and use system("start " + argx); (providing system() doesn't barf at that syntax)


The reasoning behind this requirement is I'm trying to run a large .NET program from within a legacy DOS program with very limited resources (using "start" doesn't work, but using an additional intermediary gets around the problem.)
Dec 17 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
OK 2 methods for you.

You sound like you are semi familiar with strings so use a string to create your string, then use the member function string::c_str() to return a pointer to an array of characters containing the string.

Alternitively us the C standard library functions

strcpy(...)
strcat(...)

To copy all the arguments into a single string. In order to make sure the string is long enough you should really calculate how much space you need and allocate the memory use malloc. The required space will be

sum of the lengths of all arguments + number of arguments

the length of a single argument can be calculated using strlen(...)


However initially (and may be alternitively) just use a very large array that is unlikely to be exceeded.
Dec 19 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.