![]() |
C++: Arbitrary File Input
I can open a file when the filename is hard coded or if I ask the user and store the filename into a string. But I have no idea how to open a file when the filename is only specified at run-time on the command line. For instance:
myProg filename1 myProg filename2 etc., myProg needs to input the text from the various files for processing but I don't know how to get at the contents. This isn't redirection either which I would be fine with. Any ideas? |
It's been years since I programmed in C, but isn't this part of the main function? Like, argv() and argc()?
|
Yup, you need to use the argc and argv parameters to main. argc is the parameter count, and argv is an array of strings that are the command-line parameters tokenized by whitespace.
|
Thanks... something I hadn't seen before.
|
I believe array slot 2 has the first command line specified? The first one is the name of the program or something?
|
Well, technically it's array slot 1, since C/C++/C# use zero-based indexing ;-)
So if argc == 1, there are no arguments, just the name of the program in argv[0]. |
All times are GMT -5. The time now is 10:57 AM. |
Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.