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

相关推荐
hansang_IR4 分钟前
【题解】P9753 [CSP-S 2023] 消消乐
数据结构·c++·算法
liliangcsdn6 分钟前
基于信号强度的稀疏选股算法的探索分析
开发语言·python
竞赛考级题库9 分钟前
202606 青少年等级考试C/C++真题一级 建议答题时长:60min
java·c语言·c++
4SAPI11 分钟前
2026年大模型API接入选型指南:企业与个人用户的架构、稳定性与成本考量
大数据·开发语言·数据库·人工智能·架构·php
lmy_loveF15 分钟前
go 切换go version 版本
开发语言·后端·golang
小白快快跑哦20 分钟前
python-字符串全解(四):字符串方法
开发语言·python·字符串
linx29521 分钟前
阶段五讲义:STL 全貌——扔掉手写循环,学“动词“
c++
handler0141 分钟前
【Linux】动态库、静态库使用与原理
linux·运维·服务器·c++·c·动态库·静态库
raindayinrain43 分钟前
c++并发--同步
c++·同步
大模型丫丫1 小时前
LangGraph + MCP(Model Context Protocol)完整讲解
java·开发语言·数据库