Appending to a File using system()
Question posted by: Reggie
(Guest)
on
July 11th, 2006 02:35 PM
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.
10
Answers Posted
Reggie wrote:
Quote:
Originally Posted by
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
Reggie wrote:
Quote:
Originally Posted by
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
Reggie wrote:
Quote:
Originally Posted by
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!!!
Reggie wrote:
Quote:
Originally Posted by
>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!!!
Thanks for the help everyone. Adding ">>" to the command is just what
I needed.
Reggie wrote:
Quote:
Originally Posted by
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")
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:
Quote:
Originally Posted by
Reggie wrote:
Quote:
Originally Posted by
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")
Reggie wrote:
Quote:
Originally Posted by
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
On Tue, 11 Jul 2006 18:03:37 -0700, Reggie wrote:
Quote:
Originally Posted by
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 *");
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 196,994 network members.
Top Community Contributors
|