开发者

Removing Unwanted Characters Fom CGI Output

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);
}


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).


'ÿ' 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜