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

C

CodeGrommet

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/clibrary/cstdlib/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_test.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;
}
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Jorgen Grahn

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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


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

Jim Langston

CodeGrommet said:
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/clibrary/cstdlib/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_test.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");
 
C

CodeGrommet

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?
 
C

CodeGrommet

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_test.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;
}
 
B

BobR

CodeGrommet said:
/* 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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top