By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

I want to write a simple log file app in c++

P: n/a
I'm testing out the system() command. I coded the following. How can
I output the results into the given output file? Your help will be
appreciated.

/* system example : DIR */
//code based on: http://www.cplusplus.com/reference/c...ib/system.html

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{
//opening the log file

ofstream myLog;
myLog.open("C:\\unisa\\MyFiles\\Test_bin\\myLog_te st.txt");

//executing sytem() command

int i;
cout<<("Checking if processor is available...")<<endl;
if (system(NULL)) puts ("Ok");
else exit (1);
cout<<("Executing command DIR...\n")<<endl;
i=system ("dir"); //executing the DOS system command
cout<<"The value returned was: "<<i<<endl;

//closing the log file

myLog.close();

return 0;
}

Aug 11 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2007-08-11 20:33, CodeGrommet wrote:
I'm testing out the system() command. I coded the following. How can
I output the results into the given output file? Your help will be
appreciated.
Generally, system() should be avoided. If you have to execute external
programs uses some of the platform-specific sys-calls designed for this
purpose. If you want to keep the code portable stick to the sys-calls
defined in POSIX, such as exec() etc.

--
Erik Wikström
Aug 11 '07 #2

P: n/a
On Sat, 11 Aug 2007 19:19:25 GMT, Erik Wikström <Er***********@telia.comwrote:
On 2007-08-11 20:33, CodeGrommet wrote:
>I'm testing out the system() command. I coded the following. How can
I output the results into the given output file? Your help will be
appreciated.

Generally, system() should be avoided. If you have to execute external
programs uses some of the platform-specific sys-calls designed for this
purpose.
I'd put it this way: if you're going to use system(), be aware how it
works on your target platforms. It's not trivial, with the
interactions with the Unix shell and everything ...
If you want to keep the code portable stick to the sys-calls
defined in POSIX, such as exec() etc.
But they are messy (and POSIX-specific).

Someone should (or maybe already has?) implement the popen/subprocess
stuff from Python -- some kind of limited portability, but also access
to the best the OS can provide.

(Or the best of that stuff; the Python people got it wrong multiple
times, so there is half a dozen interfaces to choose from in the
standard Python library, with different weaknesses)

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Aug 12 '07 #3

P: n/a
On 2007-08-12 19:15, Jorgen Grahn wrote:
On Sat, 11 Aug 2007 19:19:25 GMT, Erik Wikström <Er***********@telia.comwrote:
>On 2007-08-11 20:33, CodeGrommet wrote:
>>I'm testing out the system() command. I coded the following. How can
I output the results into the given output file? Your help will be
appreciated.

Generally, system() should be avoided. If you have to execute external
programs uses some of the platform-specific sys-calls designed for this
purpose.

I'd put it this way: if you're going to use system(), be aware how it
works on your target platforms. It's not trivial, with the
interactions with the Unix shell and everything ...
>If you want to keep the code portable stick to the sys-calls
defined in POSIX, such as exec() etc.

But they are messy (and POSIX-specific).

Someone should (or maybe already has?) implement the popen/subprocess
stuff from Python -- some kind of limited portability, but also access
to the best the OS can provide.
A bit off-topic, but ever tried man 3 popen on a POSIX compatible
machine? It's a but limited since it's only one way communication but I
seem to recall that you can create your own two-way communication popen
quite easily.

--
Erik Wikström
Aug 12 '07 #4

P: n/a
"CodeGrommet" <ri*********@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
I'm testing out the system() command. I coded the following. How can
I output the results into the given output file? Your help will be
appreciated.

/* system example : DIR */
//code based on:
http://www.cplusplus.com/reference/c...ib/system.html

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{
//opening the log file

ofstream myLog;
myLog.open("C:\\unisa\\MyFiles\\Test_bin\\myLog_te st.txt");

//executing sytem() command

int i;
cout<<("Checking if processor is available...")<<endl;
if (system(NULL)) puts ("Ok");
else exit (1);
cout<<("Executing command DIR...\n")<<endl;
i=system ("dir"); //executing the DOS system command
cout<<"The value returned was: "<<i<<endl;

//closing the log file

myLog.close();

return 0;
}
Is this what you are looking for?

i = system("dir c:\\unisa\\MyFiles\\Test_bin\\myLog_test.txt");
Aug 13 '07 #5

P: n/a
On Aug 13, 4:01 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>
Is this what you are looking for?

i = system("dir c:\\unisa\\MyFiles\\Test_bin\\myLog_test.txt");
Yes, that's what I was looking for. Thank you. I'm using a mingW
compiler on a win XP platform and I get the following output:

Checking if processor is available...
Ok
Executing command DIR...

The process cannot access the file because it is being used by another
process.
The value returned was: 1
Press any key to continue . . .
Thus my program fails. Is it perhaps because system() is incompatible
with xp security? How can I dump the contents of my console to a text
file?
Aug 13 '07 #6

P: n/a
On Aug 13, 11:28 am, CodeGrommet <rick.sof...@gmail.comwrote:
On Aug 13, 4:01 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
Is this what you are looking for?
i = system("dir c:\\unisa\\MyFiles\\Test_bin\\myLog_test.txt");

Yes, that's what I was looking for. Thank you. I'm using a mingW
compiler on a win XP platform and I get the following output:

Checking if processor is available...
Ok
Executing command DIR...

The process cannot access the file because it is being used by another
process.
The value returned was: 1
Press any key to continue . . .

Thus my program fails. Is it perhaps because system() is incompatible
with xp security? How can I dump the contents of my console to a text
file?
Sorry, my bad. Fixed the problem by:

/* system example : DIR */
#include <iostream>
//#include <fstream // unnecessary
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{
//opening the log file

// ofstream myLog; // unnecessary
// myLog.open("C:\\unisa\\MyFiles\\Test_bin\\myLog_te st.txt"); //
unnecessary

//executing sytem() command

int i;
cout<<("Checking if processor is available...")<<endl;
if (system(NULL)) puts ("Ok");
else exit (1);
cout<<("Executing command DIR...\n")<<endl;
i = system("dir c:\\unisa\\MyFiles\\Test_bin\
\myLog_test_1.txt"); //executing the DOS system command
cout<<"The value returned was: "<<i<<endl;

//closing the log file
// myLog.close(); // unnecessary

return 0;
}

Aug 13 '07 #7

P: n/a

CodeGrommet <ri*********@gmail.comwrote in message...
/* system example : DIR */
#include <iostream>
file://#include <fstream // unnecessary
// #include <stdio.h // C
#include <cstdio // C++

// #include <stdlib.h // C
#include <cstdlib // C++

// using namespace std;

You are opening the whole std namespace, so, there is no need to 'adjust'
your code. Otherwise you might need 'std::' on some 'calls'. For this
program, put the 'using ....' inside main().
int main (){
using namespace std;
file://opening the log file
// ofstream myLog; // unnecessary
// myLog.open("C:\\unisa\\MyFiles\\Test_bin\\myLog_te st.txt"); //
unnecessary
file://executing sytem() command

int i;
cout<<("Checking if processor is available...")<<endl;
if (system(NULL)) puts ("Ok");
else exit (1);
cout<<("Executing command DIR...\n")<<endl;
i = system("dir c:\\unisa\\MyFiles\\Test_bin\
\myLog_test_1.txt"); file://executing the DOS system command
cout<<"The value returned was: "<<i<<endl;
file://closing the log file
// myLog.close(); // unnecessary
return 0;
}
--
Bob R
POVrookie
Aug 13 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.