简体   繁体   English

c ++使用系统命令问题

[英]c++ using system command problem

hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. 嗨,每个人都有一个奇怪的问题我用c ++编写代码成功编译并成功运行。 i compiled with following command g++ 1.c -o abc to run program i use ./abc now my problem is that i write a another code in c++ like 我使用以下命令g ++ 1.c -o abc编译运行程序我使用./abc现在我的问题是我在c ++中写了另一个代码

#include <fstream>
#include<iostream>
using namespace std;
int main()
{
  ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
  SaveFile <<"<configuration>";
  SaveFile<<endl;
  SaveFile<<"<property>";
  SaveFile<<endl;
  savefile.close();
  return 0;
}

now i want to run abc in this code how to do this ? 现在我想在这段代码中运行abc如何做到这一点? how to use or run abc in this file? 如何在此文件中使用或运行abc? how to use ./abc in this program ? 如何在这个程序中使用./abc?

Actually, your question title ("... using system ...") says it all. 实际上,你的问题标题(“......使用系统...”)说明了一切。 Use: 采用:

system ("./abc");

to run the ./abc program. 运行./abc程序。

There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable. 还有其他方法可以在程序中运行程序(通常依赖于特定于平台的功能),但这是最便携的。


A full sample program, testprog.cpp , to show what I mean: 一个完整的示例程序testprog.cpp ,用于显示我的意思:

#include <cstdlib>
int main (void) {
    std::system ("ls -ald m*");
    return 0;
}

Compiling this with: 用以下内容编译:

g++ -Wall -Wextra -pedantic -o testprog testprog.cpp

and running the resultant executable testprog , this outputs (on my Ubuntu 10.04 box): 并运行生成的可执行testprog ,这输出(在我的Ubuntu 10.04框上):

drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder

In other words, it runs the ls -ald m* command from within the program itself. 换句话说,它从程序本身运行ls -ald m*命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM