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?
Firstly, I'd like to say that I like the idea of files with labeled
content. Easier to read in a year or so when you've forgotten exactly
what those numbers mean.
I'm not offering a solution, but a point of departure. I haven't tried
compiling any of the incomplete code I'm posting here. It's just an
idea. Not a very robust idea. And being more robust might be important.
Or perhaps the exact order of the data in the file is important to you,
in which case this might not be the way to go.
Class FileInfo {
typedef std::map<std::string,std::string> Map;
Map m_;
public:
..
..
.. // ctors etc
..
bool readfile(const std::string &filename) {
// this might be a ctor instead of a function
m_.clear();
// open the file and...
std::string line;
while(std::getline(infile, line)) {
std::istringstream inline(line);
std::string name;
std::string equal;
std::string value;
// should check results here...
inline >> name >> equal >> value;
// might want to check things here too.
m_[name] = value;
}
return true;
}
int getInt(const std::string &name, const int defaultValue) const {
// look up the name as the key in m_ and if we find it,
// convert the associated value to an int and return it.
// if we can't do any of that, then either return the
// default value or just fail or throw.
}
... // not to hard to do the same for other numeric
... // types if you need to.
... // If values ever are strings with whitespace you may
... // want to rewrite readfile a little.
};
....Meanwhile, in another part of the program...
class MyClass {
int w,h,s;
public:
....
MyClass(const FileInfo &f) :
w( f.getInfo("w",0) ),
h( f.getInfo("h",0) ),
s( f.getInfo("S",0) )
{}
....
};
I hope this is enough to go on. It's not a perfect idea, I'm sure there
are better ways to do it, but work out the details and I suspect this
will do some reasonable amount of work for you.
And if you do decide to go with a command line, then you can always
adapt the idea, 'programname /h 34 /w 23 /S 2' by taking the arguments
an putting them into a FileInfo class. Maybe FileInfo isn't a great
name. Make some member function in FileInfo like,
void insert(const std::string &name, const std::string &value) { ... }
and you'll be able to reuse the example ctor I wrote for MyClass.
I don't think it's much of a leap to extend the idea for multiple
classes and being able to write what you read if you need to.
However, it's not entirely clear from your post what you want to
accomplish. If, for example, you have to use the particular format you
posted, so I'm not sure this is the kind of thing you need. If not,
then if you'll ask more specific questions, then you'll probably get
better answers.
HTH.
LR