Copying bytes from txt file

Jun 5, 2013 at 11:50am
Hello. I want to copy the last 4 bytes from a specific line from a text file (ie, always line 5). What is the best method to use once I open the file for reading. ie, How do I jump to line 5, then copy the last 4 bytes.
Jun 5, 2013 at 12:18pm
Read 5 lines.
Get the length of line 5.
Back up 4 characters from the end of the line.

What is so hard?

If the records were fixed length you could calculate the postion and position directly to the spot, but you stated this was a text file so the assumption is the lines are variable length.
Jun 5, 2013 at 12:45pm

1
2
3
4
5
6
7
8
9
10
11
size_t n = 5;

std::string line;

while ( n && std::getline( YourFileStream, line ) ) --n;

if ( n == 0 )
{
   line.assign( line, line.size() < 4 ? 0 : line.size() - 4, std::string::npos );
   // other stuff with line
}  
Topic archived. No new replies allowed.