开发者

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

开发者 https://www.devze.com 2022-12-17 01:12 出处:网络
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 t开发者_高级运维he

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 t开发者_高级运维here 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'.


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.


string s = File.ReadAllText("test.txt");
string[] split = s.Split(s[s.Length - 1]);


StreamReader has the ReadToEnd() method. It reads the complete file into a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号