繁体   English   中英

使用C CGI提供HTML模板/表单

[英]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外,用户不会知道其中的差异。

编辑,因为我不希望被标记为'模糊':

/* 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM