Question

Another 'new' thing I've had to consider tonight:

int main(argc, char *argv[])

I can't see why I'd want to read in from the cmd line, but you never know! Why wouldn't I just use getline and read it into a string? Or getch if it were something else?

I suspect the answer is something to do with the amount of code to process - using something like this over #include <string> is bound to use less computational power, correct?

EDIT: A year on looking back at this I feel pretty silly, had never really been taught about cmd line input for programs at university (I know right, what kinda course am I on..) :)

Was it helpful?

Solution 2

Being a games programmer, I can't see why I'd want to read in from the cmd line, but you never know!

Usually, for a GUI program, things passed in via command line tend to be hidden or debugging options.

For example, IE has a -k command line switch that cannot be set in the normal preferences. That switch makes IE run in kiosk mode with lots of stuff disabled. This is something regular users should never want to do because if you run IE in kiosk mode you have no access to anything else (it's like turning Windows into MsIeOS).

Google chrome has a command line argument --first-run that forces it to re-initialize the user preferences and pop up the first run settings dialog. This is very useful if you happen to corrupt your Chrome preferences.

Games are similar. Halo2 has a -windowed command line argument that starts the game in a window instead of fullscreen. Here's a list of Halo2 command line arguments: http://support.microsoft.com/kb/830487

If you can't think of any use of command line arguments then don't use it. That doesn't mean that other people can't find a use for it.

For various programs, just google "[program name] command line arguments" to find out what, if any, there are. You'll find programs written by people who, like you, can't think of any use for command line arguments and so the programs don't have any.

OTHER TIPS

argc and argv have nothing to do with standard input and output. If i say

myapp Drink More Ovaltine

argc would be 4, and argv would contain the name of the program, then "Drink", then "More", then "Ovaltine".

getline just reads in data from an input stream. It won't ever see the command line args. (Not unless you got them from argv and put them into a stringstream, anyway. And even then, what would be the point?)

As for #include, that has nothing to do with runtime I/O either. It's for telling the compiler (or, generally, the preprocessor) to include a C source file during compilation. You can't just #include an arbitrary binary or something. (When the includes are done, the resulting bunch of characters have to form valid C++ code). And by runtime, the file's already been included -- by runtime, the app doesn't even know that file exists, and thus won't even think about reading it in.

Short version:

  • argv and argc are for access to the stuff in the command line used to start your app. It's not related to I/O at all.
  • getline is used for input from a stream at runtime, and can read pretty much any file.
  • #include is used for inserting C++ code at compile time, and can only really be used for source code.

As you can hopefully see by now, the three mechanisms are entirely separate and unrelated, and one can almost never be used in place of another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top