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

相关推荐
一杯美式 no sugar1 分钟前
C++入门基础
开发语言·c++
大鹏说大话2 分钟前
AI 辅助编程革命:如何利用 GitHub Copilot 等工具重塑开发效率
开发语言
rit84324993 分钟前
有限元法求转子临界转速的MATLAB实现
开发语言·matlab
echome8883 分钟前
Python 异步编程实战:asyncio 核心概念与最佳实践
开发语言·网络·python
剑海风云5 分钟前
JDK 26之安全增强
java·开发语言·安全·jdk26
左左右右左右摇晃7 分钟前
Java并发——多线程
java·开发语言·jvm
AMoon丶8 分钟前
Golang--内存管理
开发语言·后端·算法·缓存·golang·os
阿里嘎多学长8 分钟前
2026-03-22 GitHub 热点项目精选
开发语言·程序员·github·代码托管
23.10 分钟前
【Java】字符串底层与常量池演变全解析
java·开发语言·jvm
美式请加冰14 分钟前
异常的介绍和使用
开发语言·c++