Thread: C++ for CGI scripts

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    C++ for CGI scripts

    All my previous CGI scripts have been written in C, and I'm told that it can be done in any language that writes to STDIO. C++ writes to SDTIO, but I've never seen it used in scripts. I think OOP could be very useful in my next script, and Java applets are out of the question. What's the situation with browser's handling C++, anybody know?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: C++ for CGI scripts

    Originally posted by Sean
    All my previous CGI scripts have been written in C, and I'm told that it can be done in any language that writes to STDIO. C++ writes to SDTIO, but I've never seen it used in scripts. I think OOP could be very useful in my next script, and Java applets are out of the question. What's the situation with browser's handling C++, anybody know?
    Why wouldn't it work? Its compiled just as C is. A processor doesn't differentiate between a program written in C, and one in C++.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    May I ask: How can you write CGI scripts in C?
    none...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How can you write CGI scripts in C?
    It's surprisingly simple, here is a CGI program that takes a message and creates a simple web page:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OPEN "<HTML><BODY>"
    #define CLOSE "</BODY></HTML>"
    
    void parseMsg ( char *buf, char *s )
    {
      strcpy ( buf, s ); /* Insert a more suitable parse routine here */
    }
    
    int main ( int argc, char **argv )
    {
      char msg[BUFSIZ];
    
      if ( argc > 1 ) {
        parseMsg ( msg, argv[1] );
    
        printf ( "Content-type: text/html\n\n" );
        printf ( "%s\n%s\n%s\n", OPEN, msg, CLOSE );
      }
    
      return 0;
    }
    Or the C++ equivalent:
    Code:
    #include <iostream>
    #include <string>
    
    const char OPEN[] = "<HTML><BODY>";
    const char CLOSE[] = "</BODY></HTML>";
    
    void parseMsg ( std::string& buf, char *s )
    {
      buf = s; /* Insert a more suitable parse routine here */
    }
    
    int main ( int argc, char **argv )
    {
      std::string msg;
    
      if ( argc > 1 ) {
        parseMsg ( msg, argv[1] );
    
        std::cout<<"Content-type: text/html\n\n";
        std::cout<< OPEN <<'\n'<< msg <<'\n'<< CLOSE <<std::endl;
      }
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free complete Scripts
    By anderson in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2006, 04:18 AM
  2. scripts files
    By hiya in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2005, 02:22 PM
  3. Making scripts run in a window...
    By PanZer_Jester in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 04:53 PM
  4. how do I test my CGI scripts on my computer?
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-26-2001, 05:57 PM