Reputation: 1
I have a website back-end written in C which pastes HTML header and footer templates together along with dynamically generated content in between. For some reason, an unwanted 'ÿ' (umlaut-ed y) character (ASCII 152) is appended after every call to displayTemplate(). This character is unwanted and not part of the file. How can this be prevented from being outputted? Thanks.
The code which performs this function looks something like this:
#include <stdio.h>
#include <stdlib.h>
void displayTemplate(char *);
int main(void) {
printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1", 13, 10);
displayTemplate("templates/mainheader.html");
/* begin */
printf("<p>Generated site content goes here.</p>");
/* end */
displayTemplate("templates/mainfooter.html");
return 0;
}
void displayTemplate(char *path) {
char currentChar;
FILE *headerFile = fopen(path, "r");
do {
currentChar = fgetc(headerFile);
putchar(currentChar);
} while(currentChar != EOF);
fclose(headerFile);
}
Upvotes: 0
Views: 155
Reputation: 477040
Change your loop:
while (true)
{
currentChar = fgetc(headerFile);
if (currentChar == EOF) break;
putchar(currentChar);
}
There are probably better ways than reading byte by byte (e.g. read the entire file, or read in chunks of 64kB).
Upvotes: 2
Reputation: 798626
'ÿ'
is 255 in ISO 8859-1. Stop trying to print the EOF. The EOF is all ones in binary representation, and when cut down to 8 bits it's 255.
Upvotes: 0