使用 C CGI 服务 HTML templates/forms

Using a C CGI to serve HTML templates/forms

是否可以在 C(或 C++)CGI 脚本中调用/显示 HTML 模板(无需将原始 HTML 代码嵌入到脚本中)。

例如....

"http://localhost/cgi-bin/c-program.cgi" 将在浏览器中提供 "/var/www/cgi-bin/hello-world.html",就像它是一个独立的网页一样。除了 URL,用户不会知道其中的区别。

已编辑,因为我不希望被标记为 'vague':

/* C-PROGRAM.CGI - displays parsed html */
#include <stdio.h>
#include <stdlib.h>

int main (void) {
    FILE *fp;
    fp = fopen ("/var/www/cgi-bin/hello-world.html", "r")
    if (fp == NULL) {
         printf ("File not available, errno = %d\n", errno);
         return 1;
    }

    // Display all of parsed hello-world.html in browser

    fclose (fp);
    return 0;
}

这段代码应该能让您了解我想要实现的目标;我怎样才能实现它......如果有的话?该程序将通过 http://localhost/cgi-bin/c-program.cgi

执行

我的阅读一直引导我走上处理 HTML 的字符或行的道路......

如果你想 运行 html 的 C 脚本,你必须通过 CGI-BIN 来做(你已经知道了)或者你必须用你自己的 Apache 模块来做.后者在网络上有大量关于如何操作的教程。

如果你想在样式中使用 cgi-bin,program.cgi 将简单地在你的 html 文件上使用 fopen 并使用 printf.

void print_file(FILE *f)
{
    int c;
    if (f)
    {
        while ((c = getc(f)) != EOF)
            putchar(c);
        fclose(f);
    }
}

int main()
{
    FILE *content = fopen ("/var/www/cgi-bin/hello-world.html", "r");
    FILE *header = fopen ("/var/www/cgi-bin/header.html", "r");
    FILE *footer = fopen ("/var/www/cgi-bin/footer.html", "r");

    printf("Content-Type: text/html \n\n");
    print_file(header);
    print_file(content);
    print_file(footer);
    return 0;
}