I need someone to explain to me this code step by step:

/* File config.txt:
num = 123
str = hello
flt = 12.2
*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

struct Config 
{
    int    num;
    string str;
    double flt;
	string myadd;
};

void loadConfig(Config& config) 
{
    ifstream fin("config.txt");
    string line;
    while (getline(fin, line)) 
	{
        istringstream sin(line.substr(line.find("=") + 1));
        if (line.find("num") != -1)
            sin >> config.num;
        if (line.find("str") != -1)
			sin >> config.str;
        if (line.find("flt") != -1)
            sin >> config.flt;
		if (line.find("myadd") != -1)
            sin >> config.myadd;
    }
}

int main() {
    Config config;
    loadConfig(config);
    cout << config.num << '\n';
    cout << config.str << '\n';
    cout << config.flt << '\n';
	cout << config.myadd;
	cin.get();
}

Also I would like it if you gave me a series of suggestions on inprovement, alternateves and an explanation of "regex" the code was by: nucleon

Recommended Answers

All 9 Replies

its a poorly written program.

>>istringstream sin(line.substr(line.find("=") + 1));
The program will more than likely crash if line does not contain '=' or if that symbol is the last character on the line.

>>if (line.find("num") != -1)
line.find() doesn't return -1, it returns string::npos.

its a poorly written program.

>>istringstream sin(line.substr(line.find("=") + 1));
The program will more than likely crash if line does not contain '=' or if that symbol is the last character on the line.

>>if (line.find("num") != -1)
line.find() doesn't return -1, it returns string::npos.

Thank you for your imput but you still didn't explain how it works

You're more likely to get help if you try to explain it and ask specific questions that trouble you.

I have no idea what your level of knowledge is and won't waste my time trying to explain things that you already understand.

config files have a general format as <tab name>=<value> What that program is doing is finding specific tag names and, if found, reading the value field.

Line 26 is a loop that reads all the lines in the file. Then 28-35 check for specific tag name and, when found, reads the value field of the line.

If you are still confused then compile the program for debug and use your compiler's debugger to single step through the program, then inspect the value of the variables to see what the program did.

commented: Thank You +1

Thank you +rep though you probably dont need it since your a mod

its a poorly written program.
...
line.find() doesn't return -1, it returns string::npos.

Yeah, it's a pretty rickety structure, I must admit. :( I suppose that's just another reason not to post "solutions" since the kind of thing you whip up in five minutes is likely to be error-prone and limited.

But it is my understanding that string::npos does in fact equal -1. I get my info from here. Also it works well and without warning on gcc and vc++ compilers.

Oops. I meant here.
At any rate, I've already repented. It's probably best to use string::npos. I've been relying on that online C++ reference a little too much. I like the level of detail but I guess when it's implementation detail, it's just "informative" (as the standard would say).

size_t is defined as unsigned int by Microsoft compilers and I think also by gcc. Assigning -1 to unsigned int should probably be considered undefined behavior since unsigned int can not hold a negative value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.