473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
7 3484
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
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
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
"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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
14
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java...
3
by: Chris Cioffi | last post by:
I started writing this list because I wanted to have definite points to base a comparison on and as the starting point of writing something myself. After looking around, I think it would be a...
11
by: Michael B. | last post by:
I'm still learning C so I've written a simple app which lets you make a contact list (stored as a linked list of structs), write it to a file, and read it back. It works fine, but I notice in my...
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
4
by: Thomi Aurel RUAG A | last post by:
Hy Mike Thanks for your links, unfortunately they weren't very usefull for my specific problem. Hy Grant Edwards Thanks for your hints. A simplified test programm to compare the function for...
1
by: Ron | last post by:
I need to write a little vb.net app that looks at XML files and manipulates text in them. Here is what I need to do and I have NO idea how to do something like this. here is an example file:...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.