logging output from a system command to a text file

hello everyone,

i am trying to put together a internet monitoring you utility using the ping command. i want to write this data to a file for archiving purposes should something interrupt my internet communication. my goal is to eventually make it a windows service.

here is what I have thus far:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void wait ( int seconds );

int main() {


string command;
string q;
string ping;
string ipaddress;

// get IP ADDRESS and ping it every 3 seconds to monitor internet connectivity

cout << "please enter an ip address or hostname: ";
cin >> ipaddress;

// build command
command = "ping -n 1 " + ipaddress;

// make artificial loop
while (q != "quit") {

// open file stream
ofstream outputfile;

// open file
outputfile.open("C:\\demootext.txt");

// run command
system(command.c_str());

// add two new lines to separate output
cout << "\n\n";

// wait 3 seconds
wait (3);

// write to file
outputfile.close();

}



return 0;
}

// wait function found on cplusplus.com
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}


any feedback will be extremely helpful!

Thank you everyone in advance.
system() cannot send the output of its executed command back to its caller. popen() might be what you want.
Topic archived. No new replies allowed.