Your loop that counts the number of lines reads the entire file so that the current position is at the end of the file when you begin your second loop. Consider using rewind()
before your second loop to move back to the start of the file.
Also, your test to see if fp
is NULL should go right after the open()
call. Otherwise, your code that reads the number of lines will fail.