473,795 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8355
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.ex e %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.ex e %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.c om> 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.ex e %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_Keit h) 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.ex e ";
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.ex e ";
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.ex e ", 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.ex e ";

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.ex e ";
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.ex e ", plus the trailing '\0'). There's no room
to append outputFileName.

This is a bit better:

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

but it will also break if outputFileName is too long.

Here's a safer approach:

char *editor = "notepad.ex e";
size_t len = strlen(editor) + 1 + strlen(outputFi leName) + 1;
/* "notepad.ex e" + " " + outputFileName + '\0' */
char *command = malloc(len);
if (command == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILU RE);
}
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_Keit h) 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.ex e %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\\o utput" 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********@com cast.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.mi sc
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10214
by: Copelandia Cyanescens | last post by:
Python Python 2.3.4, Windows, "clean" install, using the example code given in the documentation, I get this... Traceback (most recent call last): File "C:\projects\email\email.py", line 24, in ? server = smtplib.SMTP('mail.xxxxx.xxx') AttributeError: 'module' object has no attribute 'SMTP' I beg to differ, it most certainly does. Right? :)
19
14058
by: Lauren Wilson | last post by:
A2K app: Question: is the flagged line (<<<) below necessary. If that line is needed, what effect does it have (if any) on the fact that the very same database is the linked back end db? Dim db As DAO.Database Dim rst As DAO.Recordset
4
4222
by: Ollie | last post by:
I have wrriten a reverse proxy for a client of mine, this reverse proxy takes the url takes it attempt to access a *.XXXX and returns the the contents of an aspx web page from another internal server, the user then clicks on a button on the page that then posts data to the reverse proxy and it routes to the correct page by creating a web request and setting the method to post. (the reverse proxy is written using the IHttpHandler interface...
2
3298
by: Jeff_Mac | last post by:
Hi there. I'm a bit of a newbie, and I would appreciate any help that anyone can give me on an error I'm getting with Crystal Reports. Every time I attempt to view a report using the Crystal Report Viewer, I get a "Load Report Failed" error, and I'm not sure what I can do to resolve it. I know it's not the connection to the data -- I'm able to get a datagrid to display the data just fine after filling a dataset. I tried searching the...
11
2150
by: Thomas A | last post by:
Hi, I fill a datgrid with data from a xml document, it works fine But.... Now I will to filter the data to the grid so only the data shows from the criteria that I set. My code now is very simple to fill the grid
169
9222
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to embark on creating a nice little Small C++ compiler devoid of C++ language features that make...
3
3290
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a "Searching..." page then, refreshes and displays the table I want. I have code that grabs data from the page using cURL but when I look at the data it contains the "Searching..." page and not the table that I want. below is the code i have so far....Thanks...
1
1867
by: mlangley | last post by:
Hi there. I am trying to figure out a way to create a series of empty records in a database (ACCESS 2003). Basically, the table has 10 years of information by id. However, if there is no ID, then there is no record. I would like to make a record that simply does not have any data in it in that case. For instance if have: ID YEAR DATA a 1 xxxxx a 2 xxxxx a 3 xxxxx a ...
14
12070
by: dieselxeon | last post by:
hi everyone .. I am a student and i am learning how to create form using vb but i am stuck here since this is my school project, I need someone to help me to figgure out what is wrong with my code.also i try to create store procedure in sql 2005 and it give me This error Msg 245, Level 16, State 1, Line 2 Conversion failed when converting the varchar value '@Itinerary.ticketid' to data type int(but my ticketid value already integer not...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.