I am writing a program that is suppose to read all the lines in a .ini file and then the program will configure the network connectivity.
ex.

ip = 128.123.156.46

then it would set the ip address to 128.123.156.46

i have tried this:

while(getline(infile, TheBuffer))
{
    if(TheBuffer == "ip =")
}

but then i relised that i cant literly label each word in the file with that method.

so i thought about

char *buffer = new char [size];

but again i can label each word like that
so if i think i should read it into a vector string, but then you you get to the end of the file it will put the next word direclty next to the previes word without a enter, so i cannot use that method either.

How am i going to do this, i really thought that the new char method would work...

Thanks.

I'm not sure I understand what the problem is. Can you describe it in a different way?

Reading an INI file: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx

Use the WINAPI for that unless you plan on doing it manually and iterating through each line and constructing tokens from it.

An INI has the sections and the keys. You want to provide the key and get the value under that section for that key. GetPrivateProfileString does that.

GetPrivateProfileString(Section, Key, DefaultValue, FileName);

You can also write an Ini using WritePrivateProfileString. GetPrivateProfileInt is the same as the string version except that it automatically does the conversion from int to string for you.

If you know all the keys, iterating through them all with a for-loop and storing the values in a vector would help you lots.

@ deceptikon

I dont know if you ever made a program that is suppose to do something and that is detirmend by command in a file.

like the autorun.ini files on CD/DVD
i try to read the file but i cant read it in the correct way so that i can process the string word by word so that i can easily give diffirent integers, strings, chars values speciefied in the file. thus also doing certein functions...

I dont know if you ever made a program that is suppose to do something and that is detirmend by command in a file.

I have, many times.

i try to read the file but i cant read it in the correct way so that i can process the string word by word so that i can easily give diffirent integers, strings, chars values speciefied in the file. thus also doing certein functions...

It completely depends on the format of the file. For a simple format, such as this one:

ip=127.0.0.1
port=80
login=fooby
password=ZG9vYnk=

If I were doing it manually, I'd read a line, split it on '=', then assign the value to the appropriate runtime setting based on the key:

#include <fstream>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>

using namespace std;

class IniOption
{
public:
    IniOption(string const& s, char delim = '=');
    string const& Key() const { return _key; }
    string const& Value() const { return _value; }
private:
    string _key;
    string _value;
};

IniOption::IniOption(string const& s, char delim)
{
    istringstream iss(s);

    if (!(getline(iss, _key, delim) && getline(iss, _value)))
    {
        throw runtime_error("Invalid INI format");
    }
}

int main()
{
    ifstream in("test.txt");

    if (in)
    {
        string line;

        while (getline(in, line))
        {
            try
            {
                IniOption ini(line);

                cout << "Setting " << ini.Key() << " to '" << ini.Value() << "'" << endl;
            }
            catch (exception const& ex)
            {
                cout << ex.what() << endl;
            }
        }
    }
}

That's obviously a naive example, but you get the idea: it's all about reading a line and parsing it according to the file's format. Some formats will separate configuration settings by a category, which complicates the algorithm a bit, but it's still straightforward.

That's why I'm confused as to what your problem is. I think this task is incredibly easy, and your description of the issues holding you back (eg. "i cant literly label each word in the file with that method") make no sense to me.

what i mean with: "i cant literly label each word in the file with that method" means like you do with a vector string:

    string ipAddr;

    if(vectortstring[0] == "ip" && vectorstring[1] == "=")
    {
        ipAddr = vectorstring[2];
    }

that way you label each word. thus it is very easy to do the correct function.

And my file doesnt have a certein format eg:

        ip = 127.000.0.0

might be at the top of the file today and tomorrow i change that file with some other commands and now tbe ip is going to be somewhere in the middle or even at the bottem of the file...

Thanks i will try the way you posted.

might be at the top of the file today and tomorrow i change that file with some other commands and now tbe ip is going to be somewhere in the middle or even at the bottem of the file...

That shouldn't matter at all. For example, just read each line and process the label accordingly:

while (getline(in, line))
{
    string label = GetLabel(line);
    string value = GetValue(line);

    if (label == "ip")
    {
        ...
    }
    else if (label == "something else")
    {
        ...
    }
    else if (label == "thing")
    {
        ...
    }
    else if (label == "another thing")
    {
        ...
    }
}

Obviously this approach involves a potentially long chain of if statements, but there are ways to handle the verbosity if you have more than a handful of labels with unique processing needs. One such method is the table lookup approach:

while (getline(in, line))
{
    string label = GetLabel(line);
    string value = GetValue(line);

    options[label](value);
}

Where beforehand you set up a dictionary of labels and corresponding functions or function objects:

map<string, void(*)(string const&)> options;

options["ip"] = &SetIp;
options["something else"] = &SetSomethingElse;
options["thing"] = &SetThing;
options["another thing"] = &SetAnotherThing;

Aa thanks that might just do the trick, but i also have one of my own...

#include <string>
#include <sstream>
#include <iostream>

int main()
{
   std::string token, text("Here;is;some;text");
   std::istringstream iss(text);
   while ( getline(iss, token, ';') )
   {
      std::cout << token << std::endl;
   }
   return 0;
}

Now see this one reads the line and where there is a ';' in the line i will make a break there.

Thanks for your help, again.

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.