if(buffer[0] == '\n')
That will only work if the newline is the first character in the line, i.e. if it's a blank line. So you're only counting blank lines. It should have been:
if(buffer[strlen(buffer)] == '\n')
But as @DavidSchwartz points out, you don't need it at all.