c++生成html文件helloworld

说明:

c++生成html文件helloworld

效果图:

step1:C:\Users\wangrusheng\CLionProjects\untitled15\CMakeLists.txt

bash 复制代码
cmake_minimum_required(VERSION 3.30)
project(untitled15)

set(CMAKE_CXX_STANDARD 20)

add_executable(untitled15 main.cpp)

# 添加filesystem库支持
target_link_libraries(untitled15 PRIVATE stdc++fs)

step2:C:\Users\wangrusheng\CLionProjects\untitled15\main.cpp

c 复制代码
#include <iostream>
#include <fstream>
#include <filesystem>

int main() {
    // Create HTML content
    const char* html_content = R"(
<!DOCTYPE html>
<html>
<head>
    <title>C++ Generated HTML</title>
</head>
<body>
    <h1>Hello World from C++!</h1>
    <p>This file was generated by a C++ program.</p>
</body>
</html>
)";

    // Set filename and path
    const std::string filename = "generated_page.html";
    std::ofstream outfile(filename);

    if (outfile) {
        // Write content to file
        outfile << html_content;
        outfile.close();

        // Get and display full path
        namespace fs = std::filesystem;
        std::cout << "HTML file created at:\n"
                  << fs::absolute(filename) << std::endl;
    } else {
        std::cerr << "Failed to create file!" << std::endl;
        return 1;
    }

    return 0;
}

step3:运行

bash 复制代码
C:\Users\wangrusheng\CLionProjects\untitled15\cmake-build-debug\untitled15.exe
HTML file created at:
"C:\\Users\\wangrusheng\\CLionProjects\\untitled15\\cmake-build-debug\\generated_page.html"

Process finished with exit code 0

end

相关推荐
Lhuu(重开版43 分钟前
JS:正则表达式和作用域
开发语言·javascript·正则表达式
点云SLAM1 小时前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理
仙俊红1 小时前
Java Map 家族核心解析
java·开发语言
浅念-1 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
荔枝一杯酸牛奶2 小时前
HTML 表单与表格布局实战:两个经典作业案例详解
前端·html
code_li2 小时前
聊聊支付宝架构
java·开发语言·架构
米奇妙妙wuu3 小时前
css实现文字和边框同样的渐变色效果
css·html·css3
少控科技3 小时前
QT高阶日记01
开发语言·qt
无限进步_3 小时前
【C++】大数相加算法详解:从字符串加法到内存布局的思考
开发语言·c++·windows·git·算法·github·visual studio
“抚琴”的人3 小时前
C#上位机工厂模式
开发语言·c#