Tuesday, July 14, 2009

C++ - what's the default method of opening a file passed to main via *argv[]?

I'm a self-taught C++ programmer, and I stumbled across the second method of main(), being main(int argc, char *argv[]) of course.





The first thing I did was create a small sample program that simply displayed the value of argc and all the values of argv[] that were passed to it. After experimenting with it a bit, I noticed that argv[0] is always the command line text used to execute my program, and the other argv values contain everything else that was passed to it. My question is, where can I go to learn more about the standard methods of passing programs arguments?





I was interested about learning the following in particular:





-How are parameters starting with tags such as -o, -f, -a, and so on usually handled?





-I noticed that, if a file opens the program via a file extension associan, argv[1] contains the location of the file calling my program. How am I to know when argv[1] is a file location, and not a usual parameter? Should I check for C:\? I've just been checking for "*:\"

C++ - what's the default method of opening a file passed to main via *argv[]?
The truth of the matter is that C++ doesn't do any handling of files/flags/parameters whatsoever. When you pass extra command lines to a program, the strings themselves are passed as char arrays (char []) sequentially into the elements of argv[] (an array of char arrays). When a space occurs in the command line, that serves as a delimiter, so every time a space show up, a new char array is added to argv[] and argc is incremented. The one exception to this delimiter rule is when you use quotes on the command line. A quoted string is passed in its entirety as a single argv element. Thus, if you ran MyProgram.exe -o -f -a C:\Program Files\MyProgram\Program Data\test.dat then argv will be an array of size argc containing the following char arrays:


argv[0] = MyProgram.exe


argv[1] = -o


argv[2] = -f


argv[3] = -a


argv[4] = C:\Program


argv[5] = Files\MyProgram\Program


argv[6] = Data\test.dat


making argc=7. Thus if you wanted to retrieve the original command line, you could do something like the following:


string argument="";


for(int i=0; i%26lt;argc; i++)


{


argument+=argv[i];


if(i!=argc-1) //add spaces to all but the last element


argument+=" ";


}


which will result in argument containing exactly what was placed on the command line. Notice how the path argument was split into multiple char arrays because of the spaces in the path. This is sometimes a big problem if not handled correctly. You will have to decide how your program will need to parse argv[] depending on expected input and how you plan on handling it. Notice also that argc contains one more than the number of command line arguments (because argv[0] is the program name itself, useful sometimes when you want to title a window or change your output like in a help printout of how to use the program depending on the name of the exe itself, meaning if the user renames the file it will still show the proper command line to use!). You have to be careful to check argc before you try parsing the command line though or you'll get out of bounds errors, leading to your program crashing or using invalid data at best, taking down other programs or even the OS at worst, though you'll need an older OS to make those happen really). Once you have properly parsed your command line and have gotten the string "C:\\Program Files\\MyProgram\\Program Data\\test.dat" finally, you can use your favorite method for opening files from path, ranging from C's FILE pointer to more complex methods in C++, MFC, Win32, ATL, or your own self-written file class. Have fun!


No comments:

Post a Comment