I found mistake. It's not really a mistake, because program was working ok. It was my wrong assumption that something was wrong because function that calls this function called for it 5 times as it should. I forgot about that because i was only thinking about this function. Shame on me. Still i have problem with reading all the required informations from .txt file. Does anyone know how to search this text line by line until match is found on exam ID(first number in line), and then from that line how to take max num of points(second number in line) and exam name(rest of text in line).