- #1
CAF123
Gold Member
- 2,948
- 88
I would like a program to read in a file entered by the user via the command line, which is then used in the main body of the code.
See example code below:
The actual toy and func's are software specific and recognised by additional #include files. If I manually hardcode run.file in toy(run.file,0) with the string FILENAME, compile and execute with a single command line parameter (./exe 1, say) then the program works. My question is, how to modify the above code so that the value FILENAME entered in the command line is read in instead as run.file? That is, to make ./exe FILENAME work? I have tried with declaring argv[1] as the argument of toy but I have not yet got this to work.
See example code below:
C++:
#include <iostream>
#include <fstream>
struct run_t {
std::string file;
};
run_t run;
const POINTER* ptr = toy(run.file,0);
int main (int argc , char* argv[])
{
run_t run;
run.file = argv[1];
std::cout << ptr->func(1,2,3) << std::endl;
return 0;
}
The actual toy and func's are software specific and recognised by additional #include files. If I manually hardcode run.file in toy(run.file,0) with the string FILENAME, compile and execute with a single command line parameter (./exe 1, say) then the program works. My question is, how to modify the above code so that the value FILENAME entered in the command line is read in instead as run.file? That is, to make ./exe FILENAME work? I have tried with declaring argv[1] as the argument of toy but I have not yet got this to work.