开发者

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. 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

#include <fstream>
#include<iostream>
using namespace std;
int main()
{
  ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/c开发者_运维技巧ore-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 ? how to use or run abc in this file? how to use ./abc in this program ?


Actually, your question title ("... using system ...") says it all. Use:

system ("./abc");

to run the ./abc program.

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:

#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):

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜