470,641 Members | 1,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

using system("xxxxx")

T O
Hi all,

I am very new to C programming and have come across problem.

My programme has opened and created a text document on the hard drive.
The document name is stored in a string called "output".

At the end of the program, I want to open the outputfile.txt
automatically using notepad.

So I figure what I want to do is

system("notepad.exe %s",output);

but of course it doesn't work.

Can anyone tell me if it is possible and if so how.

Can anyone tell me a good site that has full syntax for such command. I
have C Primer plus etc, and other books but they all lack info on such
commands.

Thanks very much in anticipation
Nov 14 '05 #1
11 2217
T O wrote:
system("notepad.exe %s",output);


The system() function invokes the native shell with the given string of
text. And it takes only one parameter, a pointer to the string which
will be given to the shell. You can solve your problem by creating the
string beforehand, like:

char foo[80]; /* This is what will hold our command */

snprintf(foo, sizeof foo, "notepad.exe %s", output);
system(foo);

Hope that helps,
--John
Nov 14 '05 #2
T O
John Valko wrote:
T O wrote:
> system("notepad.exe %s",output);


The system() function invokes the native shell with the given string of
text. And it takes only one parameter, a pointer to the string which
will be given to the shell. You can solve your problem by creating the
string beforehand, like:

char foo[80]; /* This is what will hold our command */

snprintf(foo, sizeof foo, "notepad.exe %s", output);
system(foo);

Hope that helps,
--John


Thankyou John,
I will give it a go.
Tris
Nov 14 '05 #3
John Valko <jv****@gmail.com> writes:
T O wrote:
> system("notepad.exe %s",output);


The system() function invokes the native shell with the given string
of text. And it takes only one parameter, a pointer to the string
which will be given to the shell. You can solve your problem by
creating the string beforehand, like:

char foo[80]; /* This is what will hold our command */

snprintf(foo, sizeof foo, "notepad.exe %s", output);
system(foo);


Note that the snprintf() function is new in C99 (though it may be
provided as an extension in some C90 implementations).

If your implementation doesn't provide snprintf(), you can use
sprintf() instead (without the sizeof foo argument) -- but then it's
up to you to ensure that you don't overflow the buffer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
T O wrote:
Hi all,

I am very new to C programming and have come across problem.

My programme has opened and created a text document on the hard drive.
The document name is stored in a string called "output".

At the end of the program, I want to open the outputfile.txt
automatically using notepad.

So I figure what I want to do is

system("notepad.exe %s",output);

but of course it doesn't work.

Can anyone tell me if it is possible and if so how.

Can anyone tell me a good site that has full syntax for such command. I
have C Primer plus etc, and other books but they all lack info on such
commands.

Thanks very much in anticipation


#include <stdio.h>
#include <stdlib.h>

int main(void) {
char cmnd[100];
char *out = "to.c"; /* This file is named to.c */
sprintf(cmnd, "notepad %s", out);
system(cmnd);
return 0;
}
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5
T O


Joe Wright wrote:
T O wrote:
Hi all,

I am very new to C programming and have come across problem.

My programme has opened and created a text document on the hard drive.
The document name is stored in a string called "output".

At the end of the program, I want to open the outputfile.txt
automatically using notepad.

So I figure what I want to do is

system("notepad.exe %s",output);

but of course it doesn't work.

Can anyone tell me if it is possible and if so how.

Can anyone tell me a good site that has full syntax for such command.
I have C Primer plus etc, and other books but they all lack info on
such commands.

Thanks very much in anticipation

#include <stdio.h>
#include <stdlib.h>

int main(void) {
char cmnd[100];
char *out = "to.c"; /* This file is named to.c */
sprintf(cmnd, "notepad %s", out);
system(cmnd);
return 0;
}


Thankyou all for the suggestions...I can't believe how quick the
response was.

I have to use Dev C++ for this particular project(not my choice) and
snprintf and sprintf don't appear to be in any of the libraries with it
(unless someone can tell me different).

John's response, answered my first question (i.e. only only parameter),
and gave me a clue to concatonate two strings, so I did the following.

char note[] ="notepad.exe ";
strcat(note, outputFileName);
system(note);

It works perfectly, but I am interested in any feedback or comments please.

Tris
Nov 14 '05 #6
T O wrote:
char note[] ="notepad.exe ";
strcat(note, outputFileName);
system(note);


This may work for your system, however there is a flaw in this code. On
the first line here, since you use the empty subscript the array created
will be the size needed to hold "notepad.exe ", or 13 characters. The
problem occurs when you try to append to this string. The array is big
enough to hold the first string, but the second string (assuming its
length is not 0) will go off the end of the array. If you wish to use
this approach, you should specify a size in the array declaration which
is large enough to hold both strings, such as:

char note[80] = "notepad.exe ";

As far as sprintf() and snprintf() are concerned, as Keith mentioned,
snprintf() first appears in the C99 standard, so it's conceivable that
your implementation may not have it yet. But sprintf() should be
present for sure. Both prototypes appear in stdio.h. What sort of
diagnostics do you get when you try to use them?

--John
Nov 14 '05 #7
T O <TO> writes:
[...]
Thankyou all for the suggestions...I can't believe how quick the
response was.

I have to use Dev C++ for this particular project(not my choice) and
snprintf and sprintf don't appear to be in any of the libraries with
it (unless someone can tell me different).
Can Dev C++ be told to act as a C compiler, or is it just a C++
compiler? (Most of the C language is included in C++, but there are
some subtle pitfalls.)

sprintf() is a standard C function (and C++ incorporates the C
library). Try "#include <string.h>". <OT>If you're stuck using it as
a C++ compiler, try "#include <cstring>" (I think).</OT>
John's response, answered my first question (i.e. only only
parameter), and gave me a clue to concatonate two strings, so I did
the following.

char note[] ="notepad.exe ";
strcat(note, outputFileName);
system(note);

It works perfectly, but I am interested in any feedback or comments please.


It works only by accident. You've allocated 13 characters for note
(12 for "notepad.exe ", plus the trailing '\0'). There's no room
to append outputFileName.

This is a bit better:

char note[80] = "notepad.exe ";
strcat(note, outputFileName);
system(note);

but it will also break if outputFileName is too long.

Here's a safer approach:

char *editor = "notepad.exe";
size_t len = strlen(editor) + 1 + strlen(outputFileName) + 1;
/* "notepad.exe" + " " + outputFileName + '\0' */
char *command = malloc(len);
if (command == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
sprintf(command, "%s %s", editor, outputFileName);
system(command);
free(command);

This will require #include directives for <stdio.h>, <stdlib.h>,
and <string.h>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
On Fri, 28 Jan 2005 23:53:37 +0000, T O
<> wrote:
So I figure what I want to do is

system("notepad.exe %s",output);

but of course it doesn't work.
You need to use sprint or some other form to get it into a buffer first,
then call system() with that buffer:

char buff[64];

sprintf(buff, "notepad.exe %s",output);
system(buff);

However, whether that will work (open the file you want) I don't know,
because it depends what directory notepad will look in to find the file,
you might need to find that and pass the complete file name with disk
and directory path ("\\c:\\temp\\output" for instance).
Can anyone tell me if it is possible and if so how.
The above method is valid C. Details of the program you run, its
parameters, where the file will be put and where the program you run
will look for it are better asked in a newsgroup relevant to your
platform (one of the Miscosoft or Windows newsgroups).
Can anyone tell me a good site that has full syntax for such command. I
have C Primer plus etc, and other books but they all lack info on such
commands.


You already have info on the commands you need. There is no single
command to do it, you need to break it down into "build the command
line" and "run the program" steps. (This also means that for debugging
you can print out the command line you create before (or instead of)
calling the program. I've had to do that a number of times especially
with Windows where it allos spaces in the file and path names, they can
be real pigs to pass to another program.)

Chris C
Nov 14 '05 #9
[How can I start notepad from a C programme using system("...") and
pass values of variables to notepad?]

Joe Wright <jo********@comcast.net>:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
char cmnd[100];
char *out = "to.c"; /* This file is named to.c */
sprintf(cmnd, "notepad %s", out);
system(cmnd);
return 0;
}


I consider it dangerous to simply append the filename to the
command string for system().

If the filename happens to be the string "myfile.txt & DEL /S
%HOMEPATH%", the resulting command string will be "notepad
myfile.txt & DEL /S %HOMEPATH%". Thus the cmd.exe will start
notepad on the file myfile.txt and will afterwards erase your
whole home directory!

From
http://www.microsoft.com/windowsxp/h...lloverview.asp

Note

* The ampersand (&), pipe (|), and parentheses ( ) are
special characters that must be preceded by the escape
character (^) or quotation marks when you pass them as
arguments.

If you want to append parameters that you don't know about to be
safe, you must convert them to a safely quoted form first.

xpost & f'up to comp.os.ms-windows.apps.misc
Nov 14 '05 #10
Keith Thompson <ks***@mib.org> wrote:
T O <TO> writes:
I have to use Dev C++ for this particular project(not my choice)
A good one, though.
and snprintf and sprintf don't appear to be in any of the libraries with
it (unless someone can tell me different).


Can Dev C++ be told to act as a C compiler, or is it just a C++
compiler?


Yes; in fact, I only use it as a compiler (or rather compiler
environment; Dev-C++ itself is a shell around gcc + MingW or Cygwin) for
two languages, one of which is C and the other of which is not C++.

Richard
Nov 14 '05 #11
On Sat, 29 Jan 2005 05:11:50 GMT, Keith Thompson <ks***@mib.org>
wrote:
T O <TO> writes:
[...]
I have to use Dev C++ for this particular project(not my choice) and
snprintf and sprintf don't appear to be in any of the libraries with
it (unless someone can tell me different).
<snip> sprintf() is a standard C function (and C++ incorporates the C
library). Try "#include <string.h>". <OT>If you're stuck using it as
a C++ compiler, try "#include <cstring>" (I think).</OT>

sprintf is in <stdio.h>. (Standard) C++ supports the C headers as both
<xxx.h> which puts (leaves) the names in the global namespace, and
<cxxx> which puts functions in std. It is the _C++_ headers where the
standard has only e.g. <iostream> in std not the prestandard
common-use <iostream.h> in global.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Copelandia Cyanescens | last post: by
19 posts views Thread by Lauren Wilson | last post: by
4 posts views Thread by Ollie | last post: by
169 posts views Thread by JohnQ | last post: by
3 posts views Thread by Aaron | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.