system() function call question
Jun 23, 2011 at 2:26am UTC
I am currently writing a program to manage my programming projects. It is currently only going to manage C++ projects, but I plan to expand it to other languages. My problem is that when I create a project or file, I use system() to run the gedit program to open the source code files. But when I do that, the program itself stops. Here is the code for the functions:
file.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include "global.h"
#include "file.h"
string startfile = "main.cpp" ;
File::File()
{
}
File::~File()
{
}
int File::New_Project()
{
cout << "Please enter Project Name: " ;
string ProjName;
cin >> ProjName;
Proj_Name = ProjName;
string Project = Proj_Name + ".pjt" ;
ofstream fout(Project.c_str(), ios::ate);
ofstream mout(startfile.c_str());
mout << "#include <iostream>\n" << "\n" << "int main(int argc, char* argv)\n" << "{\n" << " return 0;\n" << "}" ;
mout.close();
fout << "$PROJECT_NAME = " << Project << "\n" ;
fout << "$SOURCE_LIST:\n" ;
fout << "#main.cpp\n" ;
fout.close();
string command1 = "gedit " + startfile;
system((command1.c_str()));
return 0;
}
int File::Open_Project()
{
return 0;
}
int File::New_File()
{
string FileName;
cout << "Please choose an option: \n" <<
"1 - C++ Source \n" << "2 - CPP Header \n" <<
"> " ;
int option;
string File_Name;
loop:
cin >> option;
switch (option)
{
case 1:
{
cout << "Please enter a file name: " ;
cin >> FileName;
File_Name = FileName + ".cpp" ;
ofstream sout(File_Name.c_str());
sout.close();
break ;
}
case 2:
{
cout << "Please enter a file name: " ;
cin >> FileName;
File_Name = FileName + ".h" ;
ofstream hout(File_Name.c_str());
hout.close();
break ;
}
default :
{
cout << "Please enter a valid option.\n" << "> " ;
goto loop;
}
}
string command2 = "gedit " + File_Name;
system((command2.c_str()));
return 0;
}
int File::Open_File()
{
return 0;
}
I already tried the fork() function, and that cause my computer to nearly lock up. Does anyone have any ideas?
Jun 23, 2011 at 12:32pm UTC
I don't know if system() goes through the shell. If it does you could add an `&' to the command to launch in the background. Else learn to use fork and exec properly, it won't lock up if used properly.
Topic archived. No new replies allowed.