add "&& !buf.empty() " to the if condition to correctly process empty lines