0

Is it possible to read a text file backwards from the end of each line up until a space? I need to be able to output the numbers at the end of each line. My text file is formatted as follows:

1 | First Person | 123.45
2 | Second Person | 123.45
3 | Third Person | 123.45

So my output would be, 370.35.

cvandal
  • 764
  • 5
  • 18
  • 31
  • possible duplicate of [read file backwards (last line first)](http://stackoverflow.com/questions/6922829/read-file-backwards-last-line-first) – mvp May 05 '13 at 08:06
  • 1
    Your expected output does not match what you're saying in your question... – Moo-Juice May 05 '13 at 08:08

1 Answers1

0

Yes. But in your case, it's most likely more efficient to simply read the whole file and parse out the numbers.

You could do something like this (and I'm writing this in pseudocode so you have to acutally write real code, since that's how you learn):

   seek to end of file. 
   pos = current position
   while(pos >= 0)
   {
       read a char from file. 
       if (char == space)
       {
          flag = false;
          process string to fetch out number and add to sum. 
       }
       else
       {
          add char to string
       }
       if (char == newline)
       {
           flag = true;
       }
       pos--
       seek to pos-2
   }
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227