Find the last line and word in a file

Im trying to find the last line in a file and the last word in the line. if the last word has space . or , after it i have to get rid of it. This doesnt work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string lastword(ifstream& file)
{
    string lastword1, str;
    string lastline;
    long posend = 0;

    while(!file.eof())
    {
        getline(file, lastline);
        posend = lastline.find_last_of(' ');
    }
    lastword1 = posend;
    
    lastword1 = lastline.substr(0,lastline.length()-1); 
    
    return lastword1;
}
Break into pieces:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string wordfromline( string line ) {
  istringstream ss( line );
  // read words from ss
  // return the last
}

string lastword(ifstream& file)
{
  string word( "" );
  string line;
  while ( getline( file, line ) ) {
    string lastword = wordfromline( line );
    if ( ! lastword.empty() ) {
       word = lastword;
    }
  }
  // prune trailing dot or comma from word
  return word;
}
Topic archived. No new replies allowed.