Thread: Message Board

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    Message Board

    I am working on a simple message board written in C using CGI, but I keep getting an internal error. I have reviewed my code 100 times and still cant find it.

    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(qs != NULL)
    	{
    		if(sscanf(qs,"m=%100s",m) == 1)
    		{
    			fputs(m,log);
    		}
    	}
    	fclose(log);
    	return;
    }
    
    void recreate(void)
    {
    	FILE *log;
    	char m[101];
    	log = fopen("log.txt","r");
    	printf("Content-Type: text/html\n\n<html><head><title>Message Board</title></head><body>");
    	while(!feof(log))
    	{
    		fgets(m,sizeof m,log);
    		printf("%s</br>",m);
    	}
    	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;
    }
    Can anybody spot the problem?
    Fried chicken for everybody!
    -Kernel Sanders

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, basics... does your fopen calls succeed (check for NULL)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by Elysia View Post
    Well, basics... does your fopen calls succeed (check for NULL)?
    What do you mean? The fopen call comes before the check for NULL if thats what your asking. Besides, the check for NULL would be irrelevant to that because it checks a string from the environment variable QUERY_STRING not from a file.
    Fried chicken for everybody!
    -Kernel Sanders

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> FILE *log = fopen("log.txt","a");
    You do not check if log == NULL. Calling file functions on an unopened file is undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lruc View Post
    What do you mean? The fopen call comes before the check for NULL if thats what your asking. Besides, the check for NULL would be irrelevant to that because it checks a string from the environment variable QUERY_STRING not from a file.
    What elysia meant is that you need to check if the file actually was possible to open, and you didn't get NULL back from fopen(). If your code is running with a current directory that you can not write into, then it would fail to create the file, and crash when you try to do feof() or fgets() or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Ok heres some code with checks thrown in(and some moderate obfuscation):

    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)
    		{
    			fputs(m,log);
    		}
    	}
    	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
    	{
    		printf("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;
    }
    Fried chicken for everybody!
    -Kernel Sanders

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what erorr are you getting?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Calling fclose on a non-opened file may very well result in a crash too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you may want to either use fflush(stdout) or at least add a newline after printing the error!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    FWIW, fputs() doesn't append a newline like puts() does, so your log as generated by append() might have every message on one line . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by matsp View Post
    And what erorr are you getting?

    --
    Mats
    The strange thing is nothing when I run the program from the terminal. The only error I get is when I run it through the server, which gives no details to the error.
    Fried chicken for everybody!
    -Kernel Sanders

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by Elysia View Post
    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;
    }
    Last edited by lruc; 03-31-2009 at 01:41 PM.
    Fried chicken for everybody!
    -Kernel Sanders

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not just OK, but it's CORRECT to not close a file that you didn't succeed in opening.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lruc
    So if the fopen returns NULL its ok to not close the file?
    The file is not open, so it cannot be closed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    OK I'm one step closer. The file is outputted but the form isn't appended correctly. Also the last line in the file is printed twice. Should I use a do loop instead of a while loop?
    Fried chicken for everybody!
    -Kernel Sanders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Isn't the C Board a cool name?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 12-23-2002, 01:09 PM
  3. Message Board singatures
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-16-2002, 12:36 PM
  4. Java Message board
    By darthxmark in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 03:15 AM