• Advertisement
Sign in to follow this  

executable with arguments???

This topic is 4042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

c:> program.exe r 23 g (or something like that) I would really like to know how this is done. I’ve seen this done all the time and I don’t even know what’s it called :( I think this can only be done on a dos prompt. Apparently you can specify some arguments before the program is even launched. I usually type my main function like this: void main(int argc, char **argv) does this have anything to do with that?? Does anybody have any info on this?? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try this and it should become clear:


#include <stdio.h>
int main(int argc, char *argv[])
{
for(int i = 0; i < argc; i++)
printf("%s\n", argv);
return 0;
}


Share this post


Link to post
Share on other sites
Your main function will be defined like this:

static void Main(string[] args)

which is an array of strings, so you can program like this for instance:


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsolePipeReplacer
{
class Program
{
static void Main(string[] args)
{

string fi = args[0] as string;

if (fi == null)
return;

FileInfo fo = new FileInfo(fi);


StreamReader SR;
string S;
SR = File.OpenText(fi);
S = SR.ReadToEnd();
S = S.Replace(',', '|');
SR.Close();
SR.Dispose();
SR = null;

StreamWriter SW;
SW = File.CreateText(fo.Directory + "\\new_" + fo.Name);
SW.Write(S);
SW.Close();
SW.Dispose();
SW = null;

fo = null;
}
}
}

Share this post


Link to post
Share on other sites
You have got it correct. The arguments to the main-fuction represents arguments given to the program. argc is the amount of strings in argv and it is in turn the program name followed by the arguments.

In your example argc would be 4 and argv "program.exe", "r", "23", "g".

Share this post


Link to post
Share on other sites
The above people have already said how it can be done, but I thought I'd pick up on this:

Quote:
I think this can only be done on a dos prompt.


Not quite true. Yes, the DOS prompt is one way; but you can also provide these arguments via the "Debugging -> Command Line" option in Visual Studio (other compilers will have some equivalent); if you have a shortcut to the executable in Windows, you can also edit the shortcut itself to give command line arguments too. The first is obviously very useful for debugging, but even shortcuts are very useful once your program is finished.

Share this post


Link to post
Share on other sites
wow, thanks for the fast replay everyone. tThe links and examples were great.

I knew there was something fishy about that main function :P I always hated using things that I don't understand (I used pointers for more than 4 years before I knew what they were really for -_-)

Although, regarding the example made by Wc-duck. Is the name of the program also an argument?????

Thanks SunTzu for the shortcut tip. One more reason not to go crawling back to DOS :P

Share this post


Link to post
Share on other sites
Quote:
Original post by Farraj
Although, regarding the example made by Wc-duck. Is the name of the program also an argument?????


Yes, the name of the program will be the first element in argv[]. So, if you ran your program as "progname somearg", argv[0] would be progname and argv[1] would be somearg.

Share this post


Link to post
Share on other sites
Yes it is. At least here on my linux-box (I would be surprised if it were any different in windows).

Actually the argv-array is the command-line separated by spaces so "../../folder1/folder2/test boj bjopp" is the list "../../folder1/folder2/test", "boj", "bopp"

Edit: added last section.

Share this post


Link to post
Share on other sites
Quote:
Original post by Wc-duck
Yes it is. At least here on my linux-box (I would be surprised if it were any different in windows).


argv[0] must contain the name of the program. If the name is unavailable on the current platform, then the first character in argv[0] must be the null character ('\0'), denoting the end of the 'string'.

Share this post


Link to post
Share on other sites
Quote:
Original post by Farraj
I usually type my main function like this:

void main(int argc, char **argv) does this have anything to do with that??

Does anybody have any info on this??


Although the others have answered your question, there is still one thing that should be pointed out. In C++, the standard requires the return value of main to be an int. You still aren't required to actually have a return statement (unlike other functions), but the return type must be specified as int. However, your post doesn't make it clear which language you are using, and in C, void main is technically acceptable.

Share this post


Link to post
Share on other sites
oh sorry, I use C++

and you're right, my mistake. I usually do type it like this:

int main(int argc, char **argv)

Although it's still doesn't matter :P

??????????

You know what, it just hit me. If the arguments for the program goes to the argc and argv in the main function, where does the return value (if there's any) goes too???

Share this post


Link to post
Share on other sites
Quote:
Original post by Farraj
You know what, it just hit me. If the arguments for the program goes to the argc and argv in the main function, where does the return value (if there's any) goes too???


It is given to the shell that invoked the program. Retrieving it is an OS/shell specific thing (in other words "read the manual"). In Cmd.exe (WinXP) it is stored in the %ERRORLEVEL% variable. In bash (Linux) it is stored in the $? variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Reckoner
n C, void main is technically acceptable.


Not necessarily.

Quote:

ISO/IEC 9899:TC2 says:
5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.


I'm unsure as to why people seem to assume development of C ended in the early 90s... Sure, it is arguably unsuitable for much work today, but it was made far more modern with the recent standard revisions.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
I'm unsure as to why people seem to assume development of C ended in the early 90s... Sure, it is arguably unsuitable for much work today, but it was made far more modern with the recent standard revisions.


My post does apply to C99 and the link that I gave referred to the current standard. Read the last fragment of the paragraph you posted:

Quote:
or in some other implementation-defined manner.


If a particular implementation allows it, void main would be conforming, but not necessarily portable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement