Your loop to read the next character always sets index at 0 so when you read a character that is not the end marker you go to 1 (and then back to 0).
You need to structure the code so that you have a while loop to read a line in which you increase index