Reading complete sentences from a file.

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?
add a delimiter '.' (period) when calling the getline function. following links explains.

http://www.cplusplus.com/reference/string/string/getline/
I understand that but getline stops reading when the \n character is reached. How can i get it to read until my sentence is complete?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <algorithm>
#include <cctype>

using namespace std;

int main()
{
   string sentence;
   set <string> sentences;

  ifstream file("thisfile.txt");

  if(file.is_open())
  {
    while(!file.eof())
    {
      getline(file, sentence, ".");
      sentence.erase(remove_if(sentence.begin(), sentence.end(), IsChars("\n")), sentence.end());
      sentences.insert(sentence);
    }
  }

  file.close(); //close file

  return 0;
} // get_sentences
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()
Last edited on
Hey cubbi can you please explain the following:
1. What extra string would result if while not eof is used?
2. Why shouldn't close() be called?
thanx
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
1
2
    string sentence;
    while( getline(file, sentence, '.') )
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
Last edited on
thank you!
Topic archived. No new replies allowed.