#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标签。我的意思是,我使用表格来显示文件中的数据 但它只写标签表
最佳答案
以下建议代码:
- 干净地编译
- 执行所需的功能
- 在问题评论中包含建议的修改
- 包含代码中未使用的头文件是一种糟糕的编程习惯。 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/