Skip to main content
gamedev.net
🔒 Locked

std::getline() question

Started by Chozo 3 replies 850+ views
Original Post
Chozo
Chozo
I've got a file that can be either DOS or Unix text (so \r\n or \n delimiter depending on where it was created). I want to use std::getline(infile, string) but I can't figure out an easy way to not be bothered with differing EOL characters. Right now the \r isn't being stripped out as an EOL (since \n is the only delimiter std::getline() looks for). Is the best thing to check for the \r and remove it if it's there, or is there a cleaner way to get it done while still removing the \n?
Oluseyi
Oluseyi
AFAIK, it doesn't matter. You can always convert input files to Unix format (\n only) if you're still concerned, though.
Bincho
Bincho
You could not use getline(). If you loaded the entire file into a string, you could then use the member functions of the string to parse through the file and remove each line, including or not including blank lines.
You would use the following member functions:
substr(), find_first_of(), and erase()

You would loop through the string, finding the first of either delimiter. When you found it, you would get the substring of the beginning of the string to that delimiter, and store that in a vector of strings or whatnot. Then you'd erase the portion read. Rinse, and repeat.
TeamWhore
TeamWhore
I've actually had that problem reading console input on a cross-platform app - windows adds the \r, which screws up getline() code that works fine in linux. I never really did fix that problem, I just wrote around it :\
Chozo
Chozo
Ahhh... I even knew that reading the whole file and parsing that would be better. Thanks for reminding me. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.