Using command line parameters outside of main function

Click For Summary
SUMMARY

This discussion focuses on how to utilize command line parameters in C++ outside of the main function. The user seeks to read a filename from the command line and use it in other functions. The solution involves checking the argument count (argc) and assigning the command line input (argv[1]) to a global or accessible variable before invoking functions that require it. The final code structure demonstrates the correct placement of variable assignments to ensure proper functionality.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with command line arguments in C++
  • Knowledge of function definitions and variable scope
  • Basic understanding of pointers and structs in C++
NEXT STEPS
  • Explore C++ variable scope and lifetime
  • Learn about command line argument parsing in C++
  • Investigate the use of global variables and their implications
  • Study C++ function parameters and return types for better data handling
USEFUL FOR

C++ developers, software engineers, and students learning about command line interfaces and function design in C++. This discussion is particularly beneficial for those looking to enhance their understanding of variable scope and data flow in C++ applications.

CAF123
Gold Member
Messages
2,918
Reaction score
87
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::count <<  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.
 
Technology news on Phys.org
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.
 
  • Like
Likes   Reactions: CAF123
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::count <<  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:
CAF123 said:
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).
 
  • Like
Likes   Reactions: CAF123
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::count <<  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)
 
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   Reactions: pbuk and CAF123

Similar threads

Replies
6
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
1K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
4
Views
5K