Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve the last character of a file without reading line by line?

Tags:

c#

.net

file-io

I need to retrieve the last character of a file. It may be a line break, or one of many special characters. Can I retrieve this character without parsing through the entire file? Or is there a way that I can read an entire file into a string without worry about line breaks?

I will essentially need to split the contents of the file, based on the last character of the file. So if it is a line break, I will split the string by '\n'.

like image 427
Brandon Avatar asked Dec 18 '22 03:12

Brandon


1 Answers

You can Seek() to the end of file - 1, then just Read() one byte.

I do not have the exact functionnames and constants for the Seek at the moment, check the Stream Documentation for those.

like image 171
Morfildur Avatar answered Feb 16 '23 00:02

Morfildur