read one tank at a time until I reach a blank space in C #

advertisements

I'm really lost here, don't know what i did wrong.

StreamReader reader = new StreamReader (@"Destination.txt");
int i=0;
char[] word = new char[16];
While (reader.Peek().ToString() != " ")
{
    word[i] = (char)reader.Read(); //This is when the debugger stops, after 16 iterations.
    i++;
}

I know that for sure, that in the txt, there is always a space after a maximum of 15 characters so it shouldn't exceed the word array boundary, but it does. What am i doing wrong here? The reason i used Tostring is because it the Peek command kept returning an Int for some reason.


Peek returns the code of the char, not the char itself, so you need to convert it:

while ((char)reader.Peek() != ' ')