We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do I make a config file ?

The title says it all i want to make 1 file which can store information which my C++ program can read and use. How do I go about doing this.

4
Contributors
11
Replies
1 Week
Discussion Span
4 Years Ago
Last Updated
13
Views
Question
Answered
kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

All a config file need be is a simple text file with the values stored in it that your program will use.

The program will open the file, read and heed the values. If the user wants to change those values through the program, then it will have to also write the changes to the file.

Beyond that, it contains whatever you want or need.

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

how do i for instance say goto line 1 of the config file or go to line 50

kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

you could simply read through the file, line by line, not doing anything with the data you don't need at the moment.

If your file is formatted with every line exactly the same length, you could perhaps use the seekg( ) method to jump around to where a given line begins.

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

You could even use xml file for your configs . In my opinion its a bit easier than searching directly from text files.

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
Skill Endorsements: 0

But how do i search line by line how does my program differ 1 line from another

kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Imagine the next example:
You have a config.txt and in it u have :
@hdd=1024#
@ram=512#
You could use regex , and find the name of the variable thats betwen @ and = and the value of that variable that is betwen = and #.

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
Skill Endorsements: 0

Imagine the next example:
You have a config.txt and in it u have :
@hdd=1024#
@ram=512#
You could use regex , and find the name of the variable thats betwen @ and = and the value of that variable that is betwen = and #.

Whats regex

kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

BUMP

kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You shouldn't really "BUMP" a post. At least pretend to ask another question!

regex is something you can look up yourself, but it is not necessary for your problem. Here's an example of parsing a simple config file:

/* 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;
};

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;
        else if (line.find("str") != -1)
            sin >> config.str;
        else if (line.find("flt") != -1)
            sin >> config.flt;
    }
}

int main() {
    Config config;
    loadConfig(config);
    cout << config.num << '\n';
    cout << config.str << '\n';
    cout << config.flt << '\n';
}
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 92
Skill Endorsements: 0

Thanks +rep

kelechi96
Light Poster
47 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Years Ago by vmanes, jen140 and nucleon

Soz for upping such and old post .
But regex is -> regular expression , try searching it on google.

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1039 seconds using 2.75MB