0

There seems to be a lot of resources online regarding how to get inputs/outputs from text files in C++, but I can't find anything suggesting how to do it with parameters.

For Example, I have a file that reads a bunch of parameters like so:

NbProducts = 10;
NbPlates = 3;
NbPositions = 6;
MaxPositions = 3;
Demand = [6907,14342,36857,40961,61129,69578,72905,91977,93969,97336];

How would I pull this into a C++ program as variables? Thank you so much for help in advance.

1
  • 1
    If you know the names of the values beforehand, you can write class that will search for such strings and then read what is after it until new line character. If you dont know the names of fields beforehand(NbProducts etc), you are better off reading things into pair<string, string>, where first value is the field name, and the second value is everything after = up until new line
    – Creris
    Oct 17, 2014 at 21:10

1 Answer 1

3

Here would be one way to do it:

  1. Open a file
  2. Read a line
  3. Split that line on "="
  4. Your parameter is the first result of the split, and the value is the second result.
  5. Repeat for every line.

You may need to strip surrounding whitespace after the split. You'll need to parse the value after reading it, especially for your array.

An alternative would be storing your data in something like JSON or XML and using an existing parser to read it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.