I have a text file that in some places have newlines in the middle.

For example:

My name is Sam.

I am a novice programmer.
I go to school

in Michigan.

I use getline() to read through the entire file, and then I send it to a function to tokenize it into sentences that end in periods. My problem is that if the sentence doesn't end in a period, but rather a newline (like I go to school), it still stops rather than going on to read "I go to school in Michigan."

I know that the third parameter of a getline() function can be '\n' but for some reason that isn't working.

Any suggestions would be helpful!

Recommended Answers

All 9 Replies

std::string line;
        ifstream in("filename");
        while(getline(in,line,'.') )
        {
            size_t pos;
            // remove embedded line feeds if they exist
            while( (pos = line.find('\n')) != string::npos)
                line.erase(pos,pos);
            cout << line << ".\n";
        }

hey how about if u try declaring a ifstream fin
and the u just call fin.get(); that way it is going to read everything in the file

For some reason that code did not work. It instead went into an infinite loop. Do you have any other suggestions as to how to get rid of a newline in the middle of a paragraph?

ex:
blahblahblawords.
morewords
lastone.

should look like:

blahblahblawords
morewords lastone

when printed out without periods.

Post code because I can't see your monitor or what you did. The code I posted works ok with the test data you posted.

All I have to read in the file so far is

string s;
string delim = ".";

ifstream file(argv[2]);

while(!file.eof()){
getline(file,s);
Tokenize(s, TokenSentences, delim);
}

where tokenize is a function that parses the paragraph and puts each sentence into a vector called TokenSentences (which is empty when passed to tokenize).

You didn't read the code I posted did you ????? It contains a couple of lines that strips those '\n' from the middle of the lines. Go back and study that code again.

I used the code you posted and I carefully went through and looked up pos and npos because I didn't know what they did. But when I went back and inserted the code so that it looked like this:

while(getline(file,s,'.'))
{
size_t pos;
while((pos = s.find('\n'))!=string::npos)
    {
        s.erase(pos,pos);
    }
}

I got an infinite loop when I went and ran my program. So I still don't understand what I'm doing wrong.

nevermind I figured it out. Thank you!!

Change s.erase(pos, pos); to s.erase(pos, 1);. The second parameter tells how many characters to remove at index pos.

[EDIT]
Seems I was late ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.