Originally Posted by
Elysia
Calling fclose on a non-opened file may very well result in a crash too.
So if the fopen returns NULL its ok to not close the file? Some more revisions:
Code:
#include <stdio.h>
#include <stdlib.h>
void append(void)
{
FILE *log = fopen("log.txt","a");
char m[101];
char *qs = getenv("QUERY_STRING");
if(log != NULL && qs != NULL)
{
if(sscanf(qs,"m=%100s",m) == 1)
{
fprintf(log,"%s\n",m);
}
}
fclose(log);
return;
}
void recreate(void)
{
FILE *log = fopen("log.txt","r");
char m[101];
printf("Content-Type: text/html\n\n<html><head><title>Message Board</title></head><body>");
if(log != NULL)
{
while(!feof(log))
{
fgets(m,sizeof m,log);
printf("%s</br>",m);
}
}
else
{
puts("Error");
}
printf("<form action=\"http://98.243.228.110/cgi-bin/mb.cgi\" method=\"get\"><div><label><input name=\"m\"></label></div></form></body></html>");
fclose(log);
return;
}
int main(void)
{
append();
recreate();
return EXIT_SUCCESS;
}