以下是一个简易的 C++ 代码示例,用于创建一个基本的网站:

#include <iostream>
#include <string>

using namespace std;

void homepage() {
    cout << "<h1>Welcome to My Website</h1>" << endl;
    cout << "<p>This is the homepage of my simple website.</p>" << endl;
}

void about() {
    cout << "<h1>About Me</h1>" << endl;
    cout << "<p>I am a C++ programmer and this is my website.</p>" << endl;
}

int main() {
    string page;
    
    cout << "Enter the page you want to visit (homepage/about): ";
    cin >> page;
    
    cout << "Content-Type: text/html" << endl << endl;
    cout << "<html><body>" << endl;
    
    if (page == "homepage") {
        homepage();
    } else if (page == "about") {
        about();
    } else {
        cout << "<h1>404 Page Not Found</h1>" << endl;
    }
    
    cout << "</body></html>" << endl;
    
    return 0;
}

这个简单的网站使用 C++ 编写,并通过命令行接受用户输入来确定要访问的页面。在这个示例中,我们有两个页面:主页 (homepage) 和关于页 (about)。根据用户输入的页面,程序会显示相应的内容。如果用户输入了未知的页面,将会显示一个 404 的错误页面。

请注意,这只是一个非常基本的示例,仅用于演示 C++ 如何可以与 HTTP 协议交互。对于实际的网站开发,通常需要使用更强大的工具和框架,如 Web 服务器和前端技术来构建一个完整的网站。

用 C++ 创建一个简易网站 - 代码示例

原文地址: http://www.cveoy.top/t/topic/c2qd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录