Thread: C++ output to .htp file

  1. #1
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    C++ output to .htp file

    I've been working at C++ for a while now, and I'm just beginning to work on webpage programming with xhtml.

    Some of my most useful C++ programs have .txt files as output. Now I'm wondering if a little tweaking could produce full fledged web pages.

    Anybody have any experience, insight or suggestions?

    FYI I'm working on C++ Programming by Malik and Absolute Beginner's Guide to Creating Web Pages by Stauffer. Plus I've got about a half dozen other C++ books that I refer to.

    THANKS!

    -JM
    Huh?

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I have done something like this in other languages for work. You basically will create a class that has functions to output in divs and tables, a function for header and footer, then use a .css to do the formating such as size and color.

  3. #3
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Ok so I'm onto something, thanks - glad to know it's possible...

    Problem is I'm way ahead of myself - I don't quite understand classes, and I don't know squat about divs or .css...

    Wraithan, thanks for the specific advice! What languages have you done this in?

    I've got lots of reading and work ahead of me - any other suggestions for resources?

    Thanks -

    -JM
    Huh?

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I have done it in Java and PHP... Here is are some links I found googling, I think they are decent didn't read them fully but glanced through them:

    http://www.w3.org/MarkUp/Guide/Style - for CSS

    http://www.porjes.com/idocs/_DIV.html - for <div>

    You don't really need to make a class to do it for you, I used one because it allowed me to group my functions, but you can just use regular functions for it.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Ok, scratched some things out... this is by no means complete, nor is it the prettiest or OO way to do things...

    main.cpp
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::ofstream;
    
    void writeHeader(string title, string cssFile, ofstream &htmlFile);
    
    void writeDiv(string divClass, string contents, ofstream &htmlFile);
    
    void writeFooter(ofstream &htmlFile);
    
    int main(int argc, char *argv[])
    {
        cout<<"HTML Writter, lets see how well this works!"<<endl;
        ofstream htmlFile;
        htmlFile.open("index.html");
        writeHeader("Test","standard.css", htmlFile);
        
        htmlFile << "Here is some text not inside of div tags\n";
        
        writeDiv("blueText","Here is some text inside of div class blueText",htmlFile);
        
        writeFooter(htmlFile);
        return 0;
    }
    
    void writeHeader(string title, string cssFile, ofstream &htmlFile) {
    	
    	htmlFile << "<html>\n<head>\n<title>" << title << "</title>\n"
    			 << "<link rel=\"StyleSheet\" href=\"" << cssFile << "\" type=\"text/css\">\n"
    			 << "<body>\n";
    	
    }
    
    void writeDiv(string divClass, string contents, ofstream &htmlFile) {
    
    	htmlFile << "<div class=\"" << divClass << "\">" << contents << "</div>\n";
    	
    }
    
    void writeFooter(ofstream &htmlFile) {
    	
    	htmlFile << "</body>\n</html>\n";
    	
    }
    standard.css
    Code:
    body {
        color: red;
        background: black;
    }
    
    .blueText {
    	color: blue;	
    }
    This should be a decent starter and show you a little about div/css stuff, not much but I was bored and this sounded like something I wanted to write out.

  6. #6
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Awesome, thanks!

    I've bookmarked the links, plus I've got XHTML Example By Example by Walsh, Aaron E.; Raggett, Dave coming any day now.

    This will be great for a lot of stuff for next school year.

    Thanks

    -JM
    Huh?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You should read the XHTML 4.01 spec by W3C or at least finish the tutorial all the way to make sure any code you generate conforms to open standards.

    You might want to design a couple web pages or even a whole website by hand, to get a feel of it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM