0

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/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 ? how to use or run abc in this file? how to use ./abc in this program ?

3
  • i accept most of the answers given by you but i dont know how to tell that i accept the answer please expalin me what should i do Commented Mar 22, 2011 at 5:43
  • @user513164, acceptance has a specific meaning here - it means clicking on the big open green tick mark to the left of an answer. This will then become a filled-in big green tick mark and "reward" the owner of that answer with 15 points. Commented Mar 22, 2011 at 5:45
  • In the answer that you find best solves your problem, click on the check mark on the left side to mark an answer as correct. Commented Mar 22, 2011 at 5:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer please explain them i'm using ubuntu 10.04
@user513164: Have a look at fork() and the exec family.
i used the command but its generate an error ‘system’ was not declared in this scope tell me what should i do
@user513164, you need to include cstdlib as per the code given. Otherwise the compiler doesn't know about system(). If I comment out that line, I similarly get: testprog.cpp:3: error: ‘system’ was not declared in this scope.
If you use C++ and include cstdlib you are only guaranteed to have system in namespace std, so use std::system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.