I need to extract full sentences from a file and the save them in a set. I tried getline to get a full line and then removed the characters after the period. This method doesn't work too well if the other half of the sentence is on another line. How should i fix this?
getline stops reading when the delimiter character is reached. If you do not specify the delimiting character then the new line character is taken as the default delimiter.
Do u think this code could work for scanning the sentences. Im using get line to get a sentence from the beginning to the period. Im using another line of code to delete the "\n" that is gonna be in the middle of sentences that spill over to the next line. Thanks for any help.
close, but you're using "while not eof", so the resulting set will include one extra string.
use while(getline(...))
you can use remove instead of remove_if
and to make it look really like C++, move the string declaration closer to where it is used and don't call close()
1. What extra string would result if while not eof is used?
The empty string that getline() will produce after you run off the end of file. while(!file.eof()) is simply an error. In your case, the loop should be
or, for scope purists, for(string sentence; getline(file, sentence, '.'); )
2. Why shouldn't close() be called?
It's not an error, just bad style: the only time you need to do that in C++ is when you care about the return value/exception or when you're reusing the fstream object. Otherwise, let the destructor do it.
PS: also, removing endlines will make the words of your sentence stick together