tags:

views:

261

answers:

3

I am currently working on a program that read each line from a file and extract the word from the line using specific delimiter.

So basically my code looks like this

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argv, char **argc)
{
  ifstream fin(argc[1]);
  char delimiter[] = "|,.\n ";
  string sentence;

  while (getline(fin,sentence)) {
     int pos;
     pos = sentence.find_first_of(delimiter);
     while (pos != string::npos) {
        if (pos > 0) {
           cout << sentence.substr(0,pos) << endl;
        }
          sentence =sentence.substr(pos+1);
          pos = sentence.find_first_of(delimiter);
      }
  }
}

However my code didnot read the last word in the line. For example, my file looks like this. hello world

the output from the program is only the word "hello" but not "world" . I have use '\n' as the delimiter but why didnot it works?.

Any hint would be appreciated.

+2  A: 

getline does not save the new line character in the string. For example, if your file has the line "Hello World\n" getline will read this string "Hello World\0" So your code misses the "World".

Igonoring that sentence is not defined, you could alter your code to work like this:

#include<iostream>
#include<fstream>
using namespace std;

int main(int argv, char *argc)
{
  ifstream fin(argc[1]);
  char delimiter[]="|,.\n ";
  while (getline(fin,sentence)) {
     sentence += "\n";
     int pos;   
     pos = find_first_of(sentence,delimiter);
     while (pos != string:: npos) {
        if (pos > 0) {
           cout << sentence.substr(0,pos) << "\n";
        }
          sentence =sentence.substr(pos+1);
          pos = find_first_of(sentence,delimiter);
      }
  }
}

Note, I borrowed Bill the Lizards more elegant solution of appending the last delimiter. My previous version had a loop exit condition.

leif
+2  A: 

http://www.cplusplus.com/reference/iostream/istream/getline/

Paraphrasing:: Characters are extracted until the delimiting character (\n) is found and discarded, and the remaining characters returned.

Your string doesnt end with a \n, it is ^hello world$ so no delimiter is found and no new pos is found.

garrow
is there any other delimiter that could be used to identified end of line in this case?
or could I use code other then getline to read the \n? if there is one then what is it?
I guess you missed this from the article; 'If you don't want this character to be extracted, you can use member "get" instead.'
garrow
+1  A: 

As others have mentioned, getline doesn't return the newline character at the end. The simplest way to fix your code is to append one to the end of the sentence after the getline call.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argv, char **argc)
{
  ifstream fin(argc[1]);
  char delimiter[] = "|,.\n ";
  string sentence;

  while (getline(fin,sentence)) {
     sentence += "\n";
     int pos;
     pos = sentence.find_first_of(delimiter);
     while (pos != string::npos) {
        if (pos > 0) {
           cout << sentence.substr(0,pos) << endl;
        }
          sentence =sentence.substr(pos+1);
          pos = sentence.find_first_of(delimiter);
      }
  }
}
Bill the Lizard