03-11-2005, 05:53 PM | #1 | ||
College Starter
Join Date: Oct 2000
Location: Calgary
|
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? |
||
03-11-2005, 05:56 PM | #2 |
Pro Starter
Join Date: Jul 2001
Location: Not Delaware - hurray!
|
It's been years since I programmed in C, but isn't this part of the main function? Like, argv() and argc()?
__________________
She loves you, yeah, yeah, yeah, yeah! She loves you, yeah! how do you know? how do you know? |
03-11-2005, 06:17 PM | #3 |
Pro Starter
Join Date: Oct 2000
Location: Cary, NC
|
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.
__________________
-- Greg -- Author of various FOF utilities |
03-11-2005, 07:59 PM | #4 |
College Starter
Join Date: Oct 2000
Location: Calgary
|
Thanks... something I hadn't seen before.
|
03-11-2005, 08:26 PM | #5 |
Head Coach
Join Date: Mar 2003
Location: Hometown of Canada
|
I believe array slot 2 has the first command line specified? The first one is the name of the program or something?
|
03-11-2005, 09:15 PM | #6 |
Pro Starter
Join Date: Oct 2000
Location: Cary, NC
|
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].
__________________
-- Greg -- Author of various FOF utilities |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
|
|