I have some C# code that is working with a text file, and i can not seem to get it to work properly with blank or null (whitespace) lines.
My code:
while (!file.EndOfStream)
{
line = file.ReadLine();
bool isComment = (line[0] == '/') && (line[1] == '/');
bool isPoint = (line[0] == '(') && (line[line.Length - 1] == ')');
bool isWhiteSpace = string.IsNullOrEmpty(line);
Debug.Log("Comment: " + isComment + ", Point: " + isPoint + ", WhiteSpace: " + isWhiteSpace + "Value: '" + line + "'");
if (!isComment && !isPoint && !isWhiteSpace) { Application.Quit(); }
else if (isPoint)
{
//Strip parenthesis
line = line.Remove(line.Length - 1, 1).Remove(0, 1);
//break into float array
string[] arr = line.Split(',');
float xVal = float.Parse(arr[0]);
float yVal = float.Parse(arr[1]);
float zVal = float.Parse(arr[2]);
Vector3 currentVector = new Vector3(xVal, yVal, zVal);
results.Add(currentVector);
}
}
You can see that i happen to be doing things with Vector3. If the line is a comment line or a whitespace line, i want it to do nothing. If it notices parentheses, i want it to assume it is a Vector3 and parse it. Finally, if it is a line that is none of these, i want it to stop entirely. Here is a sample text file that i have created just with Notepad:
//This is a comment
// ... and so is this!
(0, -1.5, 3)
(1, 4, 1.23)
(3, 5, 2)
Notice that there is a gap between the second and third Vector3. In this particular case, the line is completely empty, it does not contain spaces or anything, i simply pressed [Enter][Enter] in Notepad. When my script reaches this line, it seems to trigger the file.EndOfStream boolean.... but its NOT the end of the file! How can i fix this? Is there a more appropriate condition for my while loop? I have also tried reading the line in and checking if it is null as the while condition, which is a more popular way to approach this, but this way of doing things also does not work for my case.
** Note: "file" is a variable of type StreamReader **