Jump to content
  • Advertisement
Sign in to follow this  
Farraj

executable with arguments???

This topic is 4313 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!