# reading 'commands' from a file

## Recommended Posts

Hello everyone, i have a little problem with the following: I want for my program (a game) something like a configuration file, where you can write something like this: "resolution 800 600" and the program changes its resolution to 800x600 (after restarting it, of course ;)) I thought of this syntax: name_of_command value1 value2 value3 .. ; name_of_command2 value4; Here is what I tried: 1) open the file 2) read a line (not perfect: what if the command is longer than 1 line?) 3) split the string after every ";" and store the parts in a vector1 4) split every element of the vector at every whitespace and store the results in a new vector2 5) now save everything in a vector of a class: the first element of vector2 would be class.name, the rest in an array of the class. Then I would have every command (with its values), thats in the first line of the file, in in an own object of a class. But its not working. Here's the code: (I thought it would be awesome if overload an operator for this ( :roll: ))
ifstream &operator>>(ifstream &stream, vector<command> &nCommand)
{
string line;
vector<string> parts;
vector<string> commands;

int commandPerLine;

getline(stream, line);

stringSplit(line, ";", parts); //splits the <1. argument> at every <2. argument> and stores it in <3. argument>

commandPerLine = parts.size();

for(int i=0; i < commandPerLine; i++)
{
stringSplit(parts[i], " ", commands);

nCommand.push_back(commands[0]); //the constructor of command stores the argument in command.name (in the example above it would be "name_of_command" or "resolution")

for(int j=0; j<3; j++) //the "value array" is limited to 3 values
{
nCommand.back().value[j] = commands[j+1]; //thats strange, but I don't know how to do it better :D
}
}

return stream;
}
}

(I translated the names of variables into English, so there could be something wrong, but in the original code its right..) Well, but its nor working. For example command.value[] stores not the values, but the name, or it is empty. What I think: The code is crap and I'm dumb. Please help me. I know, that there is (must be) a _very_ simple solution, but I don't get it :D And by the way: Sorry for my bad English :)

##### Share on other sites
You can use any character with std::getline, you can use this to separate the commands by semicolons. You can then use std::stringstream rather than splitting the string up.
std::ostream& operator>>(std::ostream &stream, Command &command);#include <sstream>std::ostream &operator>>(std::ostream &in, std::vector<Command> &commands){   std::string text;   while(std::getline(in, text. ';'))   {      Command command;      std::stringstream stream(text);      if(! (stream >> command) )      {          // error handling      }   }   return stream;}

Then the function that reads a command from the stream can be quite simple, because the stream will naturally split by whitespace.

##### Share on other sites
Thanks, it's working now.

Merry Christmas btw :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982370

• 10
• 9
• 15
• 24
• 11