what I want to do is read the "key"-value up til' the '=' and do a comparison.
If I use getline(ifstream, someString, '=') the first iteration will be fine, someString will have "Texture" but the next iteration is not fine because it will read from after the '=' to the next one. So next iteration would be ../Textures/player.png\nName
I appriciate any help I can get.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main(){
std::ifstream stream("test.txt");
std::string s;
std::string t("Name");
while(std::getline(stream, s, '=')){
if(t == s){
//t is the same as s
//do something
}
}
return 0;
}
This may not be exactly what you are looking for, but it may be a hint at least:
I've uses a stringstream for testing, as a substitute for a file. The code should be compatible if stream was a std::ifstream.
#include <iostream>
#include <fstream>
#include <sstream>
int main()
{
std::ifstream stream("test.txt");
std::string s;
std::string t("Name");
while (std::getline(stream, s))
{
std::string key{ "" }, value{ "" };
std::istringstream ss(s);
std::getline(ss, key, '='); // <--- Reads up to '='
std::getline(ss, value); // <--- Reads rest of line.
if (t == key) // <--- set a break point here to check key and value variables.
{
//t is the same as s
//do something
}
}
return 0;
}