c - 为什么这个 cgi 脚本不起作用,但在/var/www/cgi-bin/中执行时它可以作为独立程序完美运行?

标签 c apache cgi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char *cognome=NULL;
    char *nome=NULL;   
    char *email=NULL;
    char *password=NULL;
    char *password2=NULL;
    FILE *file_utenti=NULL;
    file_utenti=fopen("Utenti.dat","a+");
    struct utente 
    {
        char cognome[25];
        char nome[25];
        char email[80];
        char password[64];
        char password2[64];
    };
    struct utente Utente;
    file_utenti=fopen("Utenti.dat","r"); 
    if(file_utenti!=NULL)
    printf("%s\n","File aperto correttamente in lettura");
    else
    printf("%s\n","Impossibile leggere sul file utenti");
    while(fread(&Utente, sizeof(Utente), 1, file_utenti))
    {
        printf("%s\t",Utente.cognome);
        printf("%s\t",Utente.nome);
        printf("%s\t",Utente.email);
        printf("%s\t",Utente.password);
        printf("%s\t",Utente.password2);
        printf("\n");
    }

    fclose(file_utenti);
    return 0; 
}

如果我将它作为 cgi 脚本运行,它不会输入 while,但如果我在/var/www/cgi-bin/目录中运行它,它就可以完美工作。它打开文件,打印所有记录,然后退出 当然,我在我的cgi脚本中使用了html标签。我的意思是,我使用表格来显示文件中的数据 但它只写标签表

最佳答案

以下建议代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 在问题评论中包含建议的修改
  4. 包含代码中未使用的头文件是一种糟糕的编程习惯。 IE。建议删除该语句:#include <string.h>

现在,建议的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{  
    struct utente 
    {
        char cognome[25];
        char nome[25];
        char email[80];
        char password[64];
        char password2[64];
    };
    struct utente Utente;

    FILE *file_utenti=fopen("Utenti.dat","r"); 

    if( file_utenti )
        printf("%s\n","File aperto correttamente in lettura");

    else
    { // fopoen failed
        perror("Impossibile leggere sul file utenti");
        exit( EXIT_FAILURE );
    }

    while(fread( &Utente, sizeof(Utente), 1, file_utenti) )
    {
        printf("%s\t",Utente.cognome);
        printf("%s\t",Utente.nome);
        printf("%s\t",Utente.email);
        printf("%s\t",Utente.password);
        printf("%s\t",Utente.password2);
        printf("\n");
    }

    fclose(file_utenti);
    return 0; 
}

但是,该问题并未阐明每个字符串是否通过 NUL 字节终止。如果不是以 NUL 字节终止,则代码将输出垃圾。

关于c - 为什么这个 cgi 脚本不起作用,但在/var/www/cgi-bin/中执行时它可以作为独立程序完美运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53488457/

相关文章:

c - 如何计算C中unicode字符串中的字符

php - 在 Windows 7 (XAMPP) 中安装内存缓存

c - MergeSort - 编译但不给出任何输出

c - C99 中的灵活数组

python - apache2用什么权限/用户写django日志

apache - 浏览器以 UTF-8 而不是 windows-1251 显示页面

python - 将 python cgi.print_environ() 和 cgi.print_form() 的输出写入文件

python - 什么是纯英语的 WSGI 和 CGI​​?

perl - 将 Perl 嵌入 html 的最简单方法

arrays - C 程序在释放 char * 数组中的指针后崩溃