I assume that you are using Windows. Then the files should be opened in binary mode to disable newline translation:
FILE* input = fopen(infile, "rb");
FILE* output = fopen(outfile, "wb");
Here you should use the padding of the input:
fseek( input, originalpadding, SEEK_CUR);
But the most important error resulting in the error message is here:
for (int a = 0; a < n; a++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
fclose(output);
}
You are closing the output file!