Using command line parameters outside of main function

  • #1

CAF123

Gold Member
2,950
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:

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.
 

Answers and Replies

  • #2
You are first constructing your run variable with default (empty) value for file and then you call toy(run.file), so no matter what you later assign to run.file it is already too late. In your main function you should probably 1) check argc that you have at least one parameter (argc >= 2), if so then 2) set ptr = toy(argv[1], 0), and 3) evaluate ptr->func.
 
  • #3
Thanks for the reply! But do I understood you mean to just declare ptr in int main as follows?
C++:
int main (int argc , char* argv[])
{

  if (argc >= 2) {
  ptr = toy(argv[1], 0);
  };

  std::cout <<  ptr->func(1, 2, 3) << std::endl;

  return 0;
}
This of course works but actually I was wondering in general how to use the command line argument outside the main function (as I have other functions outside of main that need the value ptr->func)?
 
Last edited:
  • #4
I was wondering in general how to use the command line argument outside the main function
In general you provide access to data via variables. These can either be global variables, function parameters or, since you are using C++, member variables. The usual pattern is to let the main method (or a sub-function) parse the arguments into whatever variables you need and then pass them in as arguments for functions or object constructors that need them. For a single string parameter what you have already done may be just fine, but otherwise there are also plenty of libraries that can assist you with a more flexible parsing.

However, like much else in C++ where there are so many way to structure your program depending on your exact requirements so it is a bit difficult to give an easy answer that magically will match what you need. I will suggest you search for and look over various relevant examples and then try code some of your own and return here with specific questions if it becomes tricky. Depending on how much C/C++you already know this will likely also require you to learn more about the language in general (e.g. different types of variables, how functions are structured, and such).
 
  • #5
Thanks. Yes I had tried to access the argv[1] in the way you described through the struct, creating an object and then accessing the member. May I paste here a slightly updated version of the code in the OP:

C++:
#include <iostream>
#include <fstream>


struct run_t {


  std::string file;


};


run_t run;


const POINTER* ptr = toy(run.file,0);
//const POINTER* ptr = toy(FILENAME,0); //here hardcoded the FILENAME as a string => code works. I am trying to get it to work when I read in the filename from the first entry on the command line upon program execution

double Toy1(double a, double b, double c) {

return ptr->func(a,b,c);

};

double Toy2(double d) {

double factor = pow(d,2); //some dummy prefactor

return factor*Toy1(4,5,6);

};

int main (int argc , char* argv[])
{
  run_t run;
  run.file = argv[1];
 
  std::cout <<  Toy2(1) << std::endl;


  return 0;
}

So here I use the ptr in Toy1 and the result of that is then fed into Toy2 (with this I was trying to demonstrate why I didn't want to do simply the ptr->toy(argv[1],0) in main)
 
  • #6
Note that while your "run" variable needs to be declared before the functions are defined (as you have also done), you can assign a value to it at any time. When such a variable is used in a function its value is only evaluated when the function is called (i.e. when the code in the function "runs past" where the variable is referenced), and not at the time when the function is defined. In your latest example that means you can declare the "run" and "ptr" variable and then assign a value to them later, e.g. in main, before calling your functions.

You confusion indicate that you likely lack basic understanding on how C/C++ or similar languages work in general so I again suggest you try to get some of that covered by, say, searching for some good beginners guide. If you already know a bit of programming with another language then you may benefit from trying to find a "Beginners guide to C++ for XXX-programmers" or something similar. I am sure others here more familiar with teaching C++ can also provide a good list of learning resources.
 
  • Like
Likes pbuk and CAF123

Suggested for: Using command line parameters outside of main function

Replies
6
Views
1K
Replies
2
Views
656
Replies
2
Views
285
Replies
8
Views
769
Replies
16
Views
884
Replies
9
Views
2K
Replies
18
Views
641
Back
Top