Outputting to html

This is a discussion on Outputting to html within the C++ Programming forums, part of the General Programming Boards category; I am currently working on some webpages through www.bravehost.com and I need some help on some stuff. I am too ...

  1. #1
    C++ N00b mistformshadow's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA :D
    Posts
    4

    Question Outputting to html

    I am currently working on some webpages through www.bravehost.com and I need some help on some stuff.
    I am too lazy to make a crapload of html files on my computer and then upload them all to the server, so I am going to attempt to create them in my C++ IDE, which is Codeblocks.

    I need to enter a number for the name of the file (i.e. 1 for 1.html, and 2 for 2.html, and so on) until I type a void number to exit the loop. I need to make around 30 files. The files need to contain:
    Code:
    <html>
    <head>
    <title>Redirection</title>
    <meta http-equiv="refresh" content="0; url=http://guild.the-reincarnation.com/encyclopedia/spell/101.html">
    </head>
    <body bgcolor="black">
    <h2 style="color:white">Redirecting</h2>
    </body
    </html>
    The spell/101.html needs to be a variable, the one that I typed in, and each file needs to be saved under the same name. I do know a bit of C++, but I'm still kinda sketchy on a few things. I just barely got back into when I found out that I can use the using namespace std; function to use cin and cout functions.
    I am not 100% sure on the loops or the output functions, but that's why I posted in here .

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well the beauty of HTML is that it's standard text, which means you can create and open them with ofstream. If you know file I/O, then this should be no problem, otherwise I could post an example.

    Simply make a string, input whatever you want into it in the program, concatenate .html onto the end of the string the open the string. You've now created an HTML file which you can output code into.

    Here is some example code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
      ofstream makeHTML;
      string pageName;
      
      cout << "Enter the name you want for the webpage: ";
      cin >> pageName;    // I use cin rather than getline because you don't
                          // want spaces here.
                         
      pageName += ".html";  // Add .html to the end. You may want to do a check
                            // to see if the user already put it in.
    
      makeHTML.open(pageName.c_str());
      
      makeHTML << "<html> \n" << endl;
      makeHTML << "<head> \n <title>HTML in C++ is easy!</title> \n </head> \n" << endl;
      makeHTML << "<body> \n \n <p align=\"center\"><b><font size=\"5\">CONGRATULATIONS! ";
      makeHTML << "YOU JUST MADE AN HTML PAGE IN C++!</font></b></p> \n" << endl;
      makeHTML << "</body> \n </html>";
      
      return 0;
    }
    Last edited by SlyMaelstrom; 12-10-2005 at 07:36 PM.
    Sent from my iPadŽ

  3. #3
    C++ N00b mistformshadow's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA :D
    Posts
    4
    Ok, that is great, but I have forgotten a lot of things so I need a link to some info. I can't remember much about sytaxing, but I can see the program in my head lol.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Have you tried the cprogramming tutorials?

    If you have a specific question, feel free to ask.
    Sent from my iPadŽ

  5. #5
    C++ N00b mistformshadow's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA :D
    Posts
    4
    The URL for what I am doing is http://mistformshadow.bravehost.com/tcastatrep.html
    if you scroll down and click on the "Spell Book" link, you will see a list of spells. The HTML says to move directories in the immediate one, like to /encyclopedia/spells/name.html. The spells are in a totally different site and I just copied and pasted the source. I am redirecting these by creating short files with code saying, "Redirecting" and it takes you to the site, which is what the HTML states above. The only variables are the name of the document (1.html) and the URL inside the codes.

  6. #6
    C++ N00b mistformshadow's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA :D
    Posts
    4
    Ok, here it is .
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ofstream makeHTML;
        string pageName;
            do {
                cout << "Enter the page name:";
                cin >> pageName;
                if (pageName !="exit")  {
                pageName += ".html";
    
                makeHTML.open(pageName.c_str());
                makeHTML << "<html> \n" << endl;
                makeHTML << "<head> \n" << endl;
                makeHTML << "<title>Redirection</title> \n" << endl;
                makeHTML << "<meta http-equiv=\"refresh\" content=\"0; url=http://guild.the-reincarnation.com/encyclopedia/spell/" << pageName << ".html\"> \n"<< endl;
                makeHTML << "</head> \n" << endl;
                makeHTML << "<body bgcolor=\"black\"> \n" << endl;
                makeHTML << "<h2 stlye=\"color:white\">Redirecting</h2> \n" << endl;
                makeHTML << "</body> \n" << endl;
                makeHTML << "</html> \n" << endl;
                makeHTML.close();
                }
    	} while (pageName != "exit");
        return 0;
    }
    Last edited by mistformshadow; 12-10-2005 at 09:26 PM. Reason: cuz sly doesn't like them :P

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ofstream makeHTML;
        string pageName;
            do {
                cout << "Enter the page name:";
                cin >> pageName;
                if (pageName != "exit") {
                   pageName += ".html";
        
                   makeHTML.open(pageName.c_str());
                   makeHTML << "<html> \n" << endl;
                   makeHTML << "<head> \n" << endl;
                   makeHTML << "<title>Redirection</title>" \n << endl;
                   makeHTML << "<meta http-equiv=\"refresh\" content=\"0; url=http://guild.the-reincarnation.com/encyclopedia/spell/101.html\"> \n"<< endl;
                   makeHTML << "</head> \n" << endl;
                   makeHTML << "<body bgcolor=\"black\"> \n" << endl;
                   makeHTML << "<h2 stlye=\"color:white\">Redirecting</h2> \n" << endl;
                   makeHTML << "</body> \n" << endl;
                   makeHTML << "</html> \n" << endl;
               }
    	} while (pageName != "exit");
        return 0;
    }
    Last edited by SlyMaelstrom; 12-10-2005 at 09:08 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - C code creates dynamic HTML
    By Christie2008 in forum C Programming
    Replies: 19
    Last Post: 04-02-2008, 08:36 PM
  2. Writing an HTML Preprocessor
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2007, 09:01 AM
  3. cpp to html
    By toysoldier in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-17-2004, 10:21 AM
  4. Design + HTML
    By orbitz in forum C Programming
    Replies: 8
    Last Post: 11-21-2002, 06:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21