This code:
if ((buffidx == BUFFSIZ-1) || (c == '\r') || (c == '\n')) {
strips the carriage return/line feed characters from the incoming data.
This code:
if(card.write_file(f, (uint8_t *) buffer,buffidx) != buffidx)
assumes that the carriage return/line feed characters are there.
Simple enough fix...