executable with arguments???
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.
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;}
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:
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; } }}
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".
In your example argc would be 4 and argv "program.exe", "r", "23", "g".
The above people have already said how it can be done, but I thought I'd pick up on this:
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.
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.
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
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
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.
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.
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.
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'.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement