以下是一个简单的示例程序,用于运行cpp文件:

#include <iostream>
#include <string>
#include <cstdlib>

int main() {
    std::string filename;
    std::cout << "请输入要运行的cpp文件名(包括后缀.cpp):";
    std::cin >> filename;

    std::string command = "g++ " + filename + " -o output && ./output";
    int result = std::system(command.c_str());

    if (result == 0) {
        std::cout << "程序成功运行!" << std::endl;
    } else {
        std::cout << "程序运行出错!" << std::endl;
    }

    return 0;
}

使用方法:

  1. 将上述代码保存为一个后缀为.cpp的文件,例如"run_cpp_file.cpp"。
  2. 使用C++编译器(如g++)编译该文件,生成可执行文件。命令示例:g++ run_cpp_file.cpp -o run_cpp_file
  3. 运行生成的可执行文件。命令示例:./run_cpp_file
  4. 程序会提示你输入要运行的cpp文件名,输入要运行的cpp文件名(包括后缀.cpp)并按回车键。
  5. 程序会自动编译并运行输入的cpp文件,并在运行完成后显示运行结果。

请注意,使用此程序运行cpp文件时,需要确保已经安装了C++编译器(如g++)并已配置好环境变量

帮我写个运行cpp文件的程序 并告诉我怎么用

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

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