F
francesco
I must use a text file in a cgi program written in C.
The source of the program is the following:
#include "stdio.h"
#include "stdlib.h"
typedef enum {false,true} boolean;
boolean exist_file(char *nomefile);
int main(void)
{
FILE *fp=NULL;
int contatore=0;
// Legge il dato dal file counter.txt
if(exist_file("counter.txt"))
{
fp=fopen("counter.txt","r+");
if(fscanf(fp,"%d",&contatore)!=EOF)
// lo incrementa
contatore++;
// Si riposiziona all'inizio del file
rewind(fp);
// lo riscrive
fprintf(fp, "%d",contatore);
fclose(fp);
}
// Il file non esiste, viene creato
else
{
fp=fopen("counter.txt","w+");
contatore=1;
fprintf(fp,"%d",contatore);
fclose(fp);
}
printf("Content-Type: text/html\r\n\r\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>Prova contatore\n");
printf("</head>\n");
printf("<body>\n");
printf("<h1>Questa pagina è stata visitata %d volte</h1>\n",contatore);
printf("</body>\n");
printf("</html>\n");
return 0;
}
boolean exist_file(char *nomefile)
{
FILE *fp;
boolean exists=false;
printf("Nome file=%s\n",nomefile);
fp=fopen(nomefile,"r");
exists=(fp!=NULL);
//fclose(fp);
return exists;
}
but it doesn't work, there are some errors in the file management
functions I have used, for locally it works fine, but when I copy the
executable in the directory cgi-bin of my local apache webserver (linux
opensuse os), I get the following messages
"Server error!
The server has generated an inner error and isn't able to satisfy the
query. (Well, I translated it from italian )
Premature end of script headers: http_counter.exe "
Francesco
The source of the program is the following:
#include "stdio.h"
#include "stdlib.h"
typedef enum {false,true} boolean;
boolean exist_file(char *nomefile);
int main(void)
{
FILE *fp=NULL;
int contatore=0;
// Legge il dato dal file counter.txt
if(exist_file("counter.txt"))
{
fp=fopen("counter.txt","r+");
if(fscanf(fp,"%d",&contatore)!=EOF)
// lo incrementa
contatore++;
// Si riposiziona all'inizio del file
rewind(fp);
// lo riscrive
fprintf(fp, "%d",contatore);
fclose(fp);
}
// Il file non esiste, viene creato
else
{
fp=fopen("counter.txt","w+");
contatore=1;
fprintf(fp,"%d",contatore);
fclose(fp);
}
printf("Content-Type: text/html\r\n\r\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>Prova contatore\n");
printf("</head>\n");
printf("<body>\n");
printf("<h1>Questa pagina è stata visitata %d volte</h1>\n",contatore);
printf("</body>\n");
printf("</html>\n");
return 0;
}
boolean exist_file(char *nomefile)
{
FILE *fp;
boolean exists=false;
printf("Nome file=%s\n",nomefile);
fp=fopen(nomefile,"r");
exists=(fp!=NULL);
//fclose(fp);
return exists;
}
but it doesn't work, there are some errors in the file management
functions I have used, for locally it works fine, but when I copy the
executable in the directory cgi-bin of my local apache webserver (linux
opensuse os), I get the following messages
"Server error!
The server has generated an inner error and isn't able to satisfy the
query. (Well, I translated it from italian )
Premature end of script headers: http_counter.exe "
Francesco