Reading sentences from file with ignore()

i heed some help with ignore(). I have a text file with three lines of text:
AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS
MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC
SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.

and i need to read it into string arrays in sentences like this:
AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR.
ARAS MATTIS IUDICIUM PURUS SIT AMET FERMENTUM.
AONEC SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.

what im getting is this:
AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR
TIS IUDICIUM PURUS SIT AMET FERMENTUM
D ODIO OPERAE, EU VULPUTATE FELIS RHONCUS
any ideas? thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sakiniais(){
     sak=0;
      ifstream fv;
      fv.open("tekstas.txt");
      if(fv.fail()) cout<<"Nerastas failas.";
      else{
        while(getline(fv,sakiniai[sak],'.')){
            fv.ignore('\n');
            cout<<sakiniai[sak]<<endl;



           sak++;

         }
Last edited on
What do you think line 8 is doing?

Look at the declaration of istream.ignore():
http://www.cplusplus.com/reference/istream/istream/ignore/
std::istream::ignore
istream& ignore (streamsize n = 1, int delim = EOF);


One possible solution would be to read in the text, delimited by period (like you have it), but remove any newlines in the generated string.

https://stackoverflow.com/questions/20326356/how-to-remove-all-the-occurrences-of-a-char-in-c-string
1
2
#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());

This seems to work:
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
// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

//Replace with ifstream
std::istringstream iss{"AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\nMATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\nSED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS."};

int main()
{

    std::vector<std::string> line_vec;
    std::string a{};
    while(std::getline(iss, a, '.'))
    {
        std::replace(std::begin(a), std::end(a), '\n', ' ');
        if(a[0] == ' ') a.erase(std::begin(a));
        a.push_back('.');
        line_vec.push_back(a);
    }
    for(auto&& i : line_vec) std::cout << i << '\n';

}


Though a regular expression might also work here:
 
std::regex(R"((?:\s*)?(.+?)\.)");
Last edited on
Read the input word by word.
Append a space and the word to a string called "sentence". Well, don't append the space if it's the first word in the sentence.
If the word ends with '.' then you've reached the end of a sentence. Append the sentence to a collection of sentences and the clear sentence.
Repeat until the input is done.

Print the collection of sentences.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
   istringstream in( "AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\n"
                     "MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\n"
                     "SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n" );
   string str;
   while( in >> str ) cout << str << ( str.back() == '.' ? '\n' : ' ' );
}

AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR.
ARAS MATTIS IUDICIUM PURUS SIT AMET FERMENTUM.
AONEC SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.
Last edited on
Topic archived. No new replies allowed.