mercredi 29 novembre 2017

Can you parse information in the middle of a std::string using getline()?

Assume that I run this function below:

const std::string Utilities::nextToken(const std::string& record, size_t& 
startposition, bool& more)
{
  std::string token; 
  std::istringstream ss(record);
  std::getline(ss, token, delimiter);

  if (!token.empty())
  {
      startposition += token.size();
      more = true;
  }
  return token;
}

What I'm trying to achieve here is by sending a record "Hithere|yo|man" to this function and then read everything up until it hits a delimiter. In this case, it's a "|", but at the same time, it will keep track of the cursor location. The reason why I'd like to keep track of the cursor location is because when the function gets called again, it'll remember the cursor location and start from then. In this case, it'll read "Hithere" and increase the startposition by 7 as there are 7 characters. The next time this function runs, i'd like it to start from the next delimiter and obtain the next token, which would be "yo" and increase the startposition by another 2.

Can you parse information in the middle of a std::string using getline() and if not, could i have some suggestions on how to tackle this problem?

Aucun commentaire:

Enregistrer un commentaire