Quote:
|
Originally Posted by zsturgeon
Or if at a command line I typed 'myprog.exe -c "Stuff1" filename.txt', how could my program read that?
|
This is the simplest of your two questions. Consider the following:
#include <iostream>
using namespace std;
int
main(int argc, char *argv[]) {
cout << "application =\t" << argv[0] << endl;
for (int i = 1; i < argc; ++i)
cout << i << " =\t" << argv[i] << endl;
return 0;
}
The arguments to
main() define the number & array of strings passed to the application/executable. Note that passing strings to an application is part of the C++ language standard.
Quote:
|
For example if you use the 'Open with > program' sortof thing in a file browser, how could I read that.
|
This is much more complicated & is dependent upon the GUI environment used. In other words, Windows does it differently than GNOME which does it differently than KDE which does it differently than OS X,
etc.
By selecting a menu item, code within the browser is able to determine either by the file extension
(Windows, GNOME, KDE...) or resource fork
(OS X) which application has been predefined to interpret a particular file format. At that point, the application is spawned usually with the name of the selected file as its argument, in a similar manner to how C++ allows strings to be passed to a console application as demonstrated above. At that point, it is up to the spawned application to handle its file argument in whatever manner is appropriate.
In comparison to accepting strings as demonstrated above, GUI code can take significantly more lines of code & are dependent upon the environment used. It is left to the reader as an exercise to find code demonstrating similar functionality.
