How to read parameters from a text file in C++?

I need to read some parameters from a text file and assign them to a few variables.
variables:
file name: c:\param.txt
each line contains the number followed by a description of this number; the separator is ';'
3; //myMin
7; //myMax

int myMin, myMax;

Q#1. How to read the numbers from the file and assign them to the variables?

Please provide a working code.

Thanks a lot.
richtreeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
Here's one way:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string keyWord, theLine;
    int myMin, myMax;
    ifstream ifs("param.txt", ifstream::in );
    while ( getline( ifs, theLine ) ) {
        int val;
        val = atoi(theLine.c_str() );
        if( theLine.find("myMin") != string::npos ) {
            myMin = val;
        }
        else if( theLine.find("myMax") != string::npos ) {
            myMax = val;
        }
        else {
            cout << "unknown format: " << val << keyWord << endl;
        }
    }
}

Open in new window

0
richtreeAuthor Commented:
We can assume which line for which variable. The description is for human being. So the code just need to read up to the separator ';'
Can you check if your code can do this?
Also, would you please add some comments on your code?
Thanks a lot.
0
phoffricCommented:
I was using the keywords myMin or myMax in the line to identify what the value represents. So, the lines can be in any order. Here is a sample text file:
234; // mymy
7654; //myMax
321; //myMin

Open in new window

Here is the output using this text file:
unknown format: 234; // mymy
myMin = 321;  myMax = 7654

Open in new window

Here's code that produced above output:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string theLine; // holds one line in the text file
    int myMin, myMax; // the desired values to parse from the file
    ifstream ifs("param.txt", ifstream::in ); // open the file
    while ( getline( ifs, theLine ) ) { // read one line in until EOF
        int val; // holds a temporary value to be assigned to min or max
        val = atoi(theLine.c_str() ); // convert the first set of digits to an int
        if( theLine.find("myMin") != string::npos ) {
            myMin = val;  // Found "myMin" in line; so assign to myMin
        }
        else if( theLine.find("myMax") != string::npos ) {
            myMax = val;  // Found "myMax" in line; so assign to myMax
        }
        else {
            cout << "unknown format: " << theLine << endl;
        }
    }
    cout << "myMin = " << myMin << ";  myMax = " << myMax << endl;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Architecture

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.