从 CGI 输出中删除不需要的字符
我有一个用 C 编写的网站后端,它将 HTML 页眉和页脚模板以及其间动态生成的内容粘贴在一起。由于某种原因,每次调用 displayTemplate() 后都会附加一个不需要的“ÿ”(元音变音 y)字符 (ASCII 152)。该字符是不需要的并且不是文件的一部分。怎么才能不让它输出呢?谢谢。
执行此功能的代码如下所示:
#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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改循环:
可能有比逐字节读取更好的方法(例如读取整个文件,或以 64kB 的块读取)。
Change your loop:
There are probably better ways than reading byte by byte (e.g. read the entire file, or read in chunks of 64kB).
'ÿ'
在 ISO 8859-1 中为 255。停止尝试打印 EOF。 EOF 都是二进制表示的 1,当减少到 8 位时,它是 255。'ÿ'
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.