tags:

views:

107

answers:

3

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'.

A: 

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

Jonas Elfström
Isn't that a time- and memory-consuming way to get the last character?
Pascal Cuoq
@Pascal: You may want to reread the question. The OP has to have the entire content of the file regardless of the last character, so this would be the more efficient route.
Adam Robinson
Right, I was focusing too much on the "without parsing through the entire file" part. Seeking at the end and then at the beginning again allows to handle files that do not fit in memory, though.
Pascal Cuoq
+3  A: 
string s = File.ReadAllText("test.txt");
string[] split = s.Split(s[s.Length - 1]);
Pent Ploompuu
this will keep the last splitchar also in the entire text...You need to remove this first, before splitting!
Toad
I do not mind if the last character remains in the text. That would be valid EDI format.
Brandon
@brandon: in that case this example is correct
Toad
This is the code I am using, and it is working great. Thank you Pent Ploompuu.
Brandon
+7  A: 

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.

dbemerlin
1 byte may not equate to 1 char. It may depend on the files encoding.
Simon P Stevens
System.IO.Stream.Seek(-1,System.IO.SeekOrigin.End).ReadByte()
TJMonk15
If he's going to be reading the entire file anyway, then there's really no need to manipulate the stream.
Adam Robinson
what if your text file is Unicode, e.g. two bytes per char??
marc_s
@marc_s: These files will never be Unicode.
Brandon
Good point about Unicode. You would need to know the encoding method in order to get the last character. The last character is one or more bytes so you would need to keep stepping back through the file until you've got a complete character.
Skizz
@Brandon: In .Net all characters are Unicode. If you mean the last byte, then you should edit your question, replacing 'character' with 'byte'
Skizz
@Skizz: The in-memory representation of a string (or a character) in .NET is UTF-16, but that's not relevant to the file encoding. His files may indeed be 8-bit encoded (or any other encoding).
Adam Robinson