Appending to a File using system()

R

Reggie

I have been using the system() command to write the results of a
program to a file. This is being accomplished by the following when
running netstat for example:

system("netstat > example.txt");

When I run the command again, it completely rewrites the text file with
new results. What I would like to do is run the command again and have
the results append to the text file instead of completely rewriting the
text file. I have tried several hybrid commands using <fstream> but
nothing has worked yet. Thanks for any help.
 
M

mlimber

Reggie said:
I have been using the system() command to write the results of a
program to a file. This is being accomplished by the following when
running netstat for example:

system("netstat > example.txt");

When I run the command again, it completely rewrites the text file with
new results. What I would like to do is run the command again and have
the results append to the text file instead of completely rewriting the
text file. I have tried several hybrid commands using <fstream> but
nothing has worked yet. Thanks for any help.

This is an OS dependency, so you should ask in a group dedicated to
your OS (or in UNIX, your shell). <OT>In some environments, ">>"
appends.</OT>

Cheers! --M
 
P

Phlip

Reggie said:
system("netstat > example.txt");

When I run the command again, it completely rewrites the text file with
new results. What I would like to do is run the command again and have
the results append to the text file

system("netstat >> example.txt");
 
S

sonison.james

Reggie said:
I have been using the system() command to write the results of a
program to a file. This is being accomplished by the following when
running netstat for example:

system("netstat > example.txt");

When I run the command again, it completely rewrites the text file with
new results. What I would like to do is run the command again and have
the results append to the text file instead of completely rewriting the
text file. I have tried several hybrid commands using <fstream> but
nothing has worked yet. Thanks for any help.

Try

system("netstat >> example.txt");


Thanks and regards
SJ
 
P

Phlip

Reggie said:
I am using Windows XP.

mlimber (assuming that's who you replied to) mentioned topicality because
this question is off-topic for a C++ newsgroup. That means you would have
gotten the best answer on a WinXP group, not that we need to know what
platform you use.

C++ itself doesn't specify the >> command line operator.

And if your program is a simple "script" that manages your netstat, you
should write it in a simpler language, such as Ruby.
 
S

Sjouke Burry

Reggie said:
I have been using the system() command to write the results of a
program to a file. This is being accomplished by the following when
running netstat for example:

system("netstat > example.txt");

When I run the command again, it completely rewrites the text file with
new results. What I would like to do is run the command again and have
the results append to the text file instead of completely rewriting the
text file. I have tried several hybrid commands using <fstream> but
nothing has worked yet. Thanks for any help.
Is that C++ ?
Anyway, use the one you did first time,
then next time use
system("netstat >> example.txt")
 
R

Reggie

As a little aside, I am having a particular trouble with the DEVCON
Windows utility
command. The following line of code always produces a "devcon failed"
error.


system("devcon -m:\\127.0.0.1 findall *");


When I run the above command at the command prompt, I do not get this
error. Any ideas as to why I am only getting an error when I use this
line in my program? I'm not sure if this is a c++ problem or a
windows32 console problem but it has me baffled.
 
P

Phlip

Reggie said:
system("devcon -m:\\127.0.0.1 findall *");

The string literal "" interprets \ as an escape code, so \\ is the escape
for a literal \. Hence, you need -m:\\\\. Four backslashes.

Next time you use system, use it like this:

string cmd = "devcon -m...";
cout << cmd << endl;
system(cmd.c_str());

Then you see what you got.

And please consider switching to a softer language for this high-level
glue stuff. C++ is best used to program big hard systems, like databases
or network servers.
 
O

Owen Jacobson

As a little aside, I am having a particular trouble with the DEVCON
Windows utility
command. The following line of code always produces a "devcon failed"
error.


system("devcon -m:\\127.0.0.1 findall *");


When I run the above command at the command prompt, I do not get this
error. Any ideas as to why I am only getting an error when I use this
line in my program? I'm not sure if this is a c++ problem or a
windows32 console problem but it has me baffled.

A) Don't top-post.

B) Don't post off-topic questions; this'd be more appropriate in a Windows
programming newsgroup than in a language newsgroup.

C) Fortunately, there is a C++ issue here: Are you *sure* you're typing
the same command as you're passing to system? Hint:
printf("devcon -m:\\127.0.0.1 findall *");
 

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
473,774
Messages
2,569,596
Members
45,133
Latest member
MDACVReview
Top