mlt said:
Ian Collins said:
mlt said:
I have a file containing:
w = 23
h = 34
S = 2
I would like to read this text file into variables in a program like:
std::string file = "args.txt";
std::ifstream infile;
infile.open (&file[0]);
int w,h,S;
w = infile.??
h = infile.??
S = infile.??
But how do I read the value on each line in the file after the '=' to a
variable in my program?
Two choices, read the file line by line (using std::getline) and
tokenise the line or read the file token by token (>>).
The fist is better if the input format varies (inconsistent use of white
space for example).
Ok before I look into that, are there any better way to pass multiple
runtime arguments to an application?
"Better" is subjective. There are different ways. The two most common ways
are:
1. Command line parameters
2. Read from a file
Which can also work together with the filename to be read passed as a
command line parameter. It really depeneds on what the command line
parameters are going to be doing and your application. It is usually easier
for the end user for a non console program for the parameters to be saved to
a file by the application itself and read in when initialized. For a
console application sometimes it's better to be bassed as a command line
parameter (cat, ls, etc...).
And, it's even possible that you don't even want to pass them in or read
them but have them input in the application itself. It depends on how often
the values will change and what they represent.