char ch;
ifs.peek(); // look ahead to next char
ifs.get(ch); // read next char
std::string line;
std::getline(ifs, line, '<'); // read up to a specific char into variable line
Alternatively you could investigate either regular expressions or if your data is XML then possibly an XML parser.
ok what im asking is how do you tell it to stop at </ . What I'm trying to make is a simple filezilla ftp recoverer that retrieves your username and password for the ftp server. right now it assumes that the length of the host is 24 characters, the username is 8 characters and the password is 9 characters. Obviously this is the incorrect way of doing it since the length varies. So what im trying to make it do is just stop reading the data when it gets to </ . My code so far is
Would it be fair to say that all your data needs to be extracted from a construct such as this:
<tagname>info to be found</tagname>
?
To be honest I think your going to have to write a simple parser yourself or use a proper XML parser, or use regular expressions.
To write a simple parser yourself you will need to use the functions I mentionned in my previous post.
1 2 3 4 5
char ch;
file.get(ch); // read a single char
std::string data;
std::getline(file, data, '<'); // read up to specific char
And add whatever if() statements are required.
I would probably write a function to read up to the beginning of a tag, one to read the tag and another to read the data between the tag and its end tag.
std::ifstream ifs("file.xml");
// position the read pointer to the beginning of a tag
std::istream& read_to_start_tag(std::istream& is)
{
while(is.peek() != '<') { is.ignore(); }
return is;
}
// You could use this to read the data from a tag
std::string data;
std::getline(is, data, '>');
// test the name of the tag
const std::string password_tag_name = "password";
if(data.substr(0, password_tag_name.length()) == password_tag_name)
{
// you read the password tag
}
// Then if the tag name is the one you want read the info between this tag and the next one:
std::string value;
std::getline(is, data, '<');
Those are just fairly crude examples. You can tailor how you scan through the XML file to fit its particular format and grab what you want.
You might be better served, though, spending some time learning to use an proper XML parser that can do this kind of stuff very easily.