为什么此CGI脚本不起作用,但是在/var/www/cgi-bin/?

问题描述 投票:0回答:2
#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脚本运行,它不会输入时,但是如果我在/var/www/www/cgi-bin/目录中运行它,它可以很好地工作。它打开文件,打印所有记录,然后退出 当然,我在CGI脚本中使用了HTML标签。我的意思是,我使用表显示文件中的数据 但这只写标签表

c apache cgi
2个回答
0
投票

以下提出的代码:

  1. 清洁编译
  2. 执行所需功能
  3. 将评论中的建议修改介绍到问题
  4. 它是一种糟糕的编程实践,包括标头文件这些内容在代码中未使用。 IE。 建议删除该声明:
  5. #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; }
wo,这个问题不能澄清每个字符串是否是通过NUL字节终止的。  如果未由NUL字节终止,则代码将输出垃圾。
    

trory要为您的文件名称“ UTENTI.DAT”的绝对路径, 例如“/var/www/cgi-bin/utenti.dat”或该文件所在的任何地方。

行更改:file_utenti = fopen(“ utenti.dat”,“ a+”)

0
投票
确保文件适合每个人(chmod o+r utenti.dat)。

实际上,Web服务器的默认路径可能是 /var /www,这就是为什么它在此路径中找不到您的文件。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.