问题
I have a problem in c++, and I hope that some expert here could help me.
the problem is, there is a .txt file, that has some information. I want to write a program that uses this information.
the .txt file will be like this:
variable 1: 711
variable 2: [8 5 6 11]
variable 3: xyz zyx yyy
the program should read the values from the file and then use them, any idea how to do it ?!!
/* UPDATE */
what should be done is to read from the text file the numerical values, only the numerical values of the variables, and then use these values in the real program.
for example the value of variable 1 is read and put to a variable x, then in the program we may define int y=7*x ;
I know how to read a file, but what I don't know is how to read specific values of the variables.
Basically the program should read the file, recognize the name of the variable, get the value, and then use it.
many thanks
回答1:
Looks like you want to have some configuration-like file to store some information for your program to use. There are many implementations of config-files management (read, retrieve, store, save). I stick to small and convenient libconfig
: http://www.hyperrealm.com/libconfig/
It supports integer, floating-point and boolean variables, and arrays, lists and enclosed sections also.
回答2:
I think it's implicit in your question that the variables could have different types, such as numbers, arrays or strings. In that case, you must decide whether to use say boost::any<>
or boost::variant<>
, or create your own variable type ala:
struct Var
{
enum { Double, String, Array } Type;
Type type_;
double double_;
std::string string_;
std::vector<double> array_;
Var(Type t) : type_(t), double_(0) { } // empty double/string/array
Var(double d) : type_(Double), double_(d) { }
Var(const std::string& s) : type_(String), string_(s) { }
...
};
Alternatively, you could use C++'s inbuilt polymorphism and have a base class and a derived class for each real-world data type. It might even be enough for you to always store the textual representation, and store everything in a std::string
. However you do it, let's say you store values in a type named "Type".
It seems the name of the variables is to be specified on the left of the colon. If you know corresponding variables will be hard-coded in your program, then you can then set those pre-existing variables to the values on the right. Otherwise, you need to create variables associated with those identifier names dynamically: this can be done with a std::map<std::string, Type>
keyed on identifier. You could populate it as in:
std::map<std::string, Type> variables;
variables["age"] = Type(28);
variables["name"] = Type("Fred");
For parsing the actual text file, you can use iostreams. Read a line at a time ala:
std::string line;
while (getline(cin, line))
{
std::string::pos n = line.find(':');
if (pos != std::string::npos)
{
std::string identifier = line.substr(0, n - 1);
Type var;
{
// try parsing it as a number...
std::istringstream iss = line.substr(n + 1);
char c;
double d;
if (iss >> d && !(iss >> c))
var = d;
}
{
// try parsing it as an array...
std::istringstream iss = line.substr(n + 1);
var.clear(Array);
double d;
if (iss >> c && c == '[')
while (iss >> d)
result.array_.push_back(d);
if (iss && iss >> c && c == ']' && !(is >> c))
// parsed successfully as an array - use "result"
}
{
// it's a string...
var = line.substr(n + 1);
}
You should try to make a start on this and ask for specific help where you get stuck.
Note: you may find it easier to get a working (albeit slow) program meeting this requirement in a language such as Ruby, python or perl.
来源:https://stackoverflow.com/questions/4712194/reading-variables-from-text-file