Jump to content
  • Advertisement
Sign in to follow this  

std::ifstream using other separating character than space (' ')

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to use another character than whitespace as a separating character? Like this:
// input.txt:
"A nice string"
"Another nice string" "string 3"

// blah.cpp:
void some_func()
   std::string temp;
   std::ifstream file("input.txt");

   while(file >> temp)
     std::cout << temp << std::endl;


So this would print: A nice string Another nice string string 3 Is this possible?

Share this post

Link to post
Share on other sites
You cannot change the delimiter used by the built-in operator>>, to the best of my knowledge.

You might want std::getline, which gets up until a delimiter.

That or you could make your own function, perhaps something along the lines of the following:

std::istream &get_quoted(std::istream &source, std::string &str) {
char c = 0;
source >> c; // >> to skip whitespace
if ( c == '"' ) {
// if we read a ", store up to the next " in str
std::getline( source, str, '"' );
} else {
// if we read something that's not a ",
// put back whatever we read and read until whitespace
source >> str;
return source;

Of course a robust version would handle \ as an escape sequence, allow ' as an alternate delimiter, ...

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!