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

Appending to a File using system()

P: n/a
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 <fstreambut
nothing has worked yet. Thanks for any help.

Jul 11 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Reggie wrote:
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 <fstreambut
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

Jul 11 '06 #2

P: n/a
Reggie wrote:
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 <fstreambut
nothing has worked yet. Thanks for any help.
Try

system("netstat >example.txt");
Thanks and regards
SJ

Jul 11 '06 #3

P: n/a
Reggie wrote:
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");

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 11 '06 #4

P: n/a
I am using Windows XP.

Jul 11 '06 #5

P: n/a
Reggie wrote:
>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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 11 '06 #6

P: n/a
Thanks for the help everyone. Adding ">>" to the command is just what
I needed.

Jul 11 '06 #7

P: n/a
Reggie wrote:
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 <fstreambut
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")
Jul 12 '06 #8

P: n/a
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.

Sjouke Burry wrote:
Reggie wrote:
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 <fstreambut
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")
Jul 12 '06 #9

P: n/a
Reggie wrote:
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.

--
Phlip

Jul 12 '06 #10

P: n/a
On Tue, 11 Jul 2006 18:03:37 -0700, Reggie wrote:
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 *");

Jul 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.