I have come to the next part of my program, now i will have to be able to read info from textfiles and so on.
And i have had it pointed to me that i should use Regular Expressions for this part. I have been looking for guides on how to apply the regex to my C++ code, but havent found any, just some that only work with teaching the regex.
The things i need done is pretty simple.
Search for this, take that line, cut this off, and place the rest into this variable and so on. If anyone know any easier way to do that, please tell.
I want to search for 'building', then copy that line, which looks like 'building *buildingname*', cut the first part off, and just keep the namepart.
The rest is basicly the same as that with some variations.
Will probably just need one or two searches, the rest i would probably be able to get by going down a line at the time. (Which i dont know how to do either. All the guides i have read about reading from file has been extremly basic and hasnt thought me anything.)
Yeah, regexes for this would definitely be overkill.
To read a line from an std::ifstream into an std::string just do std::getline(file,string);
You should be able to figure out the rest. http://www.cplusplus.com/reference/string/string/
The highlights are find() and remove().
If you know the position, you can simply make a sub-string using substr() using the position that you found and then the end of the string (you don't need to know the size). Anyway, getline() is how you get stuff out of a file a line at a time...helios basically explained what you need to use...just experiment a bit and you should be able to figure it out.
How it is possible for me to read what is inside the birds?
Is there a command to tell it to search for this character on this line and read the information until you come to this character?
Maybe i should just post a sample of the text i am trying to read.
1. Is there a command to tell the pointer to go down one line, and to the start of that line?
To read a line from an std::ifstream into an std::string just do std::getline(file,string);
Example:
1 2 3 4 5 6 7 8
std::ifstream file("file.txt");
std::string line;
std::getline(file,line);
/*
If the file contained "This is\ntwo lines.\n", line now contains "This is". A
second call will make line contain "two lines.". A third call will clear line
and make file.good() return false.
*/
Is there a command to tell it to search for this character on this line and read the information until you come to this character?
Unless I'm misunderstanding your question, that would be std::string::find().
Maybe i should just post a sample of the text i am trying to read.
Hmm, can i use find to search for a string, then use getline to read that whole line from there on? So the getline works from the pointer set by find?
find will get me a position, but since i dont know how many strings are within the brids, i cant use only it. Unless i take the whole line, deletes the earlier info, then delete the two birds, and use what is left. But seems like a newbie way to do it.
building mainB.building //Simple by reading line then cutting the first 9 chars off.
{
levels mainB.levels[] //This line contains 1-9 entries that will go into mainB.levels[]
{
mainB.levels[] subB.type requires factions { subB.reqFac[] } //This line contains one of the above entries, a type (castle/city) and then a list of requirements
{
capability
{
//Here will be some tough strings to crack, but i'll probably figure that one out myself.
}
material subB.material //From here on it is easy
construction subB.construction
cost subB.cost
settlement_min subB.settlementmin
upgrades
{
subB.upgrades
}
}
mainB.levels[] subB.type requires factions { subB.reqFac[] } //Starts over again with the second of the earlier strings.
{
capability
{
}
material subB.material
construction subB.construction
cost subB.cost
settlement_min subB.settlementmin
upgrades
{
subB.upgrades
}
}
}
plugins
{
}
}