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

elementary construction +2

P: n/a
I am determining how best to call an already compiled c program from
another. The following is a program that compiles and shows the information
passed:
//Text1.cpp

#include <stdio.h>

int main(int argc, char* argv[])
{
int i;

for (i=0;i<=argc;i++)
printf("%d\t%s\n",i,argv[i]);
return (0);
}

The following was my first attempt to call this program with something that
mimicks K&R §5.10. It compiles, but my linker didn't like it. Do I put it
in a separate project and hard code the path? Is there a better way to go
about this than a system call? Do I just forget about it and go get my
nails done? MPJ

//text2.cpp

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

int main(int orange, char* apple[])
{
system("Text1 fruit flying Dutchmen");
return (0);
}
Nov 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
"Merrill & Michele" <be********@comcast.net> wrote:
I am determining how best to call an already compiled c program from
another. The following is a program that compiles and shows the information
passed:
//Text1.cpp
This is most likely not compiled as a C program, but as a C++ program.
Beware the snark; the languages are not the same.
#include <stdio.h>

int main(int argc, char* argv[])
{
int i;

for (i=0;i<=argc;i++)
printf("%d\t%s\n",i,argv[i]);
This is wrong. You're printing the contents of argv[argc], but
argv[argc] is required to be a null pointer, so it has no contents, and
the last printf() specifier of the last run through the loop invokes
undefined behaviour.
What you want is this:

for (i=0; i<argc; i++)
printf("%d\t%s\n", i, argv[i]);
return (0);
}

The following was my first attempt to call this program with something that
mimicks K&R §5.10. It compiles, but my linker didn't like it.
That's not very helpful. _How_ did your linker not like it? Presumably
it gave a more useful error message than "E042 - Code too ugly to link".
//text2.cpp

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

int main(int orange, char* apple[])
{
system("Text1 fruit flying Dutchmen");
return (0);
}


Apart from the C++ filename (and, if want your nits picked, the
unnecessary #include <stdio.h> and the silly names of main()'s arguments
(though I presume the latter was intentional)), I see no problem with
this program.

Richard
Nov 14 '05 #2

P: n/a
Merrill & Michele wrote:

I am determining how best to call an already compiled c program from
another. The following is a program that compiles and shows the information
passed:
//Text1.cpp ^^^^^^^^^
[...] //text2.cpp

^^^^^^^^^
[...]

I think you want comp.lang.c++, which is down the hall and to the left.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #3

P: n/a
Merrill & Michele wrote:
I am determining how best to call an already compiled c program from
another. The following is a program that compiles and shows the information
passed:
//Text1.cpp
Most C implementations compile source files whose
names end in ".c" (although the C language Standard does
not require this). Names ending in ".cpp" are often used
for source files containing C++, a quite different language
with some superficial similarities to C. By calling your
file "Text1.cpp" you may inadvertently have told your
compiler to expect C++ source, and this is likely to create
confusion if you're trying to write C. Double-check your
setup to make sure it's doing what you intend.
#include <stdio.h>

int main(int argc, char* argv[])
{
int i;

for (i=0;i<=argc;i++)
printf("%d\t%s\n",i,argv[i]);
The final execution of this loop produces undefined
behavior, because `argv[argc]' is guaranteed to be NULL.
When you pass NULL to the "%s" specifier, there's no
telling what might happen. Suggested fix: Re-examine
the ending condition for the loop, and consider how it
might be changed to avoid this problem.
return (0);
You know that the parentheses, though harmless, are
unnecessary, right? Good.
}

The following was my first attempt to call this program with somethingthat
mimicks K&R §5.10. It compiles, but my linker didn't like it.
When you ask for help with an error message whose
content or import you don't understand, please supply
the exact and complete text of the message. "My linker
didn't like it" isn't very informative, and requires the
reader to guess what happened. Twenty Questions was never
my favorite game.

My guess is that your linker liked it just fine, and
that you've mis-reported what went wrong.

Oh, and by the way: *my* linker doesn't like turnips.
Do I put it
in a separate project and hard code the path? Is there a better way togo
about this than a system call? Do I just forget about it and go get my
nails done? MPJ
"Project?" It seems your problem may not be with the
C language at all, but with the incantations that get your
implementation to do its thing. If so, you need help from
a newsgroup or other forum where people familiar with your
development environment (whatever it is) hang out. All we
can do here is help you with the C questions; getting the
C to execute correctly on System X or System Y or System Z
is not our forté.

Getting your nails done seems a good idea: it'll make it
easier to tear your hair out.

//text2.cpp

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

int main(int orange, char* apple[])
{
system("Text1 fruit flying Dutchmen");
One possible -- or even likely -- source of difficulty
is that although system() itself is a Standard library
function, its argument string's meaning is implementation-
specific. Depending on your platform, you might need a
system() argument like

"./Text1 fruit flying Dutchmen"
"..\\Project1\\Text1 fruit flying Dutchmen"
"MCR TEXT1 FRUIT FLYING DUTCHMEN"

... or perhaps an incantation even more bizarre. Again,
this is a question for your System X/Y/Z friends, not for
the C language folks.
return (0);
}


My (weakly founded) suspicion is that many of your
difficulties arise from trying to learn both C and the
ins and outs of an "integrated development environment"
at the same time. When something goes awry, you don't
know whether there's a problem with your C or with the
way the IDE has been configured. I'd *strongly* suggest
that you put the IDE aside until your grasp of C improves;
this will eliminate a rich source of screw-ups and thus
simplify the diagnostic process. Prepare your C source
with an ordinary editor, and run the compiler from the
command line -- I'd suggest you should even avoid the
widely-available "make" utility until you're at least to
the point of writing programs with multiple source files.

Good luck!

--
Er*********@sun.com

Nov 14 '05 #4

P: n/a
yikes! You iterate once too many times and people are talking about showing
you the door. Most of your comments were helpful and I thank you,
therefore, for all of them.

What I had to do was put the executable in the project space of the calling
program and voila. The output and my IDE you you see on this link:

http://home.comcast.net/~beckjensen/cstuff1.htm

It is precisely the headaches of implementation and IDE with which I
struggle most. I have therefore decided to write the brains of my programs
using c, and do the bells and whistles with ...let's not mention it. I
needed to know that I could pass information back and forth reliably.
Outstanding is still the question of whether a system() call is the best way
to invoke an executable. MPJ
Nov 14 '05 #5

P: n/a
Merrill & Michele wrote:
yikes! You iterate once too many times and people are talking about showing
you the door.
I warned you (as did Richard Bos) of a bug in your
code. The intent was to be helpful, to give you a heads-
up about a looming danger. If you find such warnings
unwelcome -- well, it's your funeral.

By the way, your shoelace is unti-- oh, never mind.
Most of your comments were helpful and I thank you,
therefore, for all of them.

What I had to do was put the executable in the project space of the calling
program and voila.
As I suspected, at least some of your problems arise
from the IDE and not from C itself. If you persist in
such IDEocy, you'll need to seek help elsewhere.
The output and my IDE you you see on this link:

http://home.comcast.net/~beckjensen/cstuff1.htm

It is precisely the headaches of implementation and IDE with which I
struggle most. I have therefore decided to write the brains of my programs
using c, and do the bells and whistles with ...let's not mention it. I
needed to know that I could pass information back and forth reliably.
Outstanding is still the question of whether a system() call is the best way
to invoke an executable. MPJ


In Standard C, system() is the *only* way to invoke an
executable (more precisely, to send a command to a command
processor, which may in turn invoke an executable). Picking
the best from a field of one is left as an exercise for the
student. (Picking the best without also picking the worst is
left as an exercise for the Zen master.)

LOOK OUT FOR TH-- oh, never mind.

--
Er*********@sun.com

Nov 14 '05 #6

P: n/a
Merrill & Michele wrote:
It is precisely the headaches of implementation and IDE with which I
struggle most. I have therefore decided to write the brains of my
programs using c, and do the bells and whistles with ...let's not
mention it. I needed to know that I could pass information back and
forth reliably. Outstanding is still the question of whether a
system() call is the best way to invoke an executable. MPJ


It is the ONLY way to do so in a portable manner, although as Eric
pointed out, even then the command string is likely to have
implementation-specific syntax.

All other ways require extentions to the language, and should be
brought up on a newsgroup dedicated to your particular platform.


Brian Rodenborn
Nov 14 '05 #7

P: n/a
> It is the ONLY way to do so in a portable manner, although as Eric
pointed out, even then the command string is likely to have
implementation-specific syntax.
Brian Rodenborn

....snipped...
Thanks again. Does ANSI chime in on what that command string can contain?
MPJ
Nov 14 '05 #8

P: n/a
Merrill & Michele wrote:
It is the ONLY way to do so in a portable manner, although as Eric
pointed out, even then the command string is likely to have
implementation-specific syntax.
Brian Rodenborn

...snipped...
Thanks again. Does ANSI chime in on what that command string can
contain? MPJ


From the C99 draft standard:

7.20.4.5 The system function

Synopsis

[#1]

#include <stdlib.h>
int system(const char *string);

Description

[#2] If string is a null pointer, the system function
determines whether the host environment has a command
processor. If string is not a null pointer, the system
function passes the string pointed to by string to that
command processor to be executed in a manner which the
implementation shall document; this might then cause the
program calling system to behave in a non-conforming manner
or to terminate.

Returns

[#3] If the argument is a null pointer, the system function
returns nonzero only if a command processor is available.
If the argument is not a null pointer, and the system
function does return, it returns an implementation-defined
value.


Brian Rodenborn
Nov 14 '05 #9

P: n/a
> [#2] If string is a null pointer, the system function
determines whether the host environment has a command
processor. If string is not a null pointer, the system
function passes the string pointed to by string to that
command processor to be executed in a manner which the
implementation shall document; this might then cause the
program calling system to behave in a non-conforming manner
or to terminate.
Brian Rodenborn


snipped... Thanks++. I don't see anything there that stops a guy from
using a batch file then. I changed the system command to include a trivial
batch file and got output that surprised me. First of all, the output's
wrong and secondly the "back again" test went into the same window. I know
I'm OT if I prattle on about the .bat file or the IDE; I thought it had
enough to do with this thread to be topical:
http://home.comcast.net/~beckjensen/cstuff2.htm
Nov 14 '05 #10

P: n/a
Merrill & Michele wrote:
It is the ONLY way to do so in a portable manner, although as
Eric pointed out, even then the command string is likely to
have implementation-specific syntax.


...snipped...
Thanks again. Does ANSI chime in on what that command string
can contain?


Spend some time searching the standard. The last draft is
available at the location in this sig. I recommend the text
version for easy searching.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library
Nov 14 '05 #11

P: n/a
"Merrill & Michele" <be********@comcast.net> wrote in message
news:2M********************@comcast.com...
[#2] If string is a null pointer, the system function
determines whether the host environment has a command
processor. If string is not a null pointer, the system
function passes the string pointed to by string to that
command processor to be executed in a manner which the
implementation shall document; this might then cause the
program calling system to behave in a non-conforming manner
or to terminate.


Brian Rodenborn


snipped... Thanks++. I don't see anything there that stops a guy from
using a batch file then. I changed the system command to include a
trivial
batch file and got output that surprised me. First of all, the output's
wrong and secondly the "back again" test went into the same window. I
know
I'm OT if I prattle on about the .bat file or the IDE; I thought it had
enough to do with this thread to be topical:
http://home.comcast.net/~beckjensen/cstuff2.htm


It's off-topic, anything related to your operating system and the way it
handles processes is system-specific and not discussed here. Ask in a
newsgroup relating to your specific platform.
Nov 14 '05 #12

P: n/a
"Merrill & Michele" <be********@comcast.net> wrote:
snipped... Thanks++. I don't see anything there that stops a guy from
using a batch file then. I changed the system command to include a trivial
batch file and got output that surprised me. First of all, the output's
wrong and secondly the "back again" test went into the same window.


I don't want to sound snippy, but IMO if you want to write programs
which interact with one another, you should first be rather better
acquainted with the intimacies of your OS, so that such details don't
confuse you any more. There's nothing really surprising in the
screenshot at the link you posted.

Richard
Nov 14 '05 #13

P: n/a
snip...
Spend some time searching the standard. The last draft is
available at the location in this sig. I recommend the text
version for easy searching.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library

That thought had occured to me. I was dismayed to find that they want $130
for a hard copy of ANSI. At the links I activated in your sig, I found what
I believe to be zip files for linux. I don't have a linux partition, for
reasons that are OT here. Unfortunately, my middle-class existence
constrains me not to get every reference I want. MPJ
Nov 14 '05 #14

P: n/a
> I don't want to sound snippy, but IMO if you want to write programs
which interact with one another, you should first be rather better
acquainted with the intimacies of your OS, so that such details don't
confuse you any more. There's nothing really surprising in the
screenshot at the link you posted.


In the DOS I remember, that output would be appropriate for:
echo on
dir

I believe what I typed should have given me either files in the root
directory or an error. You weren't surprised that DOS did all the output in
one window? MPJ
Nov 14 '05 #15

P: n/a
Merrill & Michele wrote:

snip...
Spend some time searching the standard. The last draft is
available at the location in this sig. I recommend the text
version for easy searching.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> C-library


That thought had occured to me. I was dismayed to find that they
want $130 for a hard copy of ANSI. At the links I activated in
your sig, I found what I believe to be zip files for linux. I
don't have a linux partition, for reasons that are OT here.
Unfortunately, my middle-class existence constrains me not to
get every reference I want. MPJ


Zip files can be expanded on almost any system known to me. You
can get good command line versions of zip and unzip at
info-zip.com. bz2 files can probably not be expanded under CP/M,
due to memory requirements. The C99 reference I gave you is
totally free.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #16

P: n/a
Merrill & Michele wrote:
snip...
Spend some time searching the standard. The last draft is
available at the location in this sig. I recommend the text
version for easy searching.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
Text file. can be accessed through your browser <http://www.eskimo.com/~scs/C-faq/top.html>
HTML.
<http://benpfaff.org/writings/clc/off-topic.html>
HTML again <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
Contains three gz files which are directly opened by my browser (Mozilla
Firefox). If you have some other browser, consider using winzip or winRAR
to open the file and then access the extracted txt/pdf/ps using the
appropriate reader
<http://www.dinkumware.com/refxc.html> C-library

HTML again :)

That thought had occured to me. I was dismayed to find that they want $130
for a hard copy of ANSI. At the links I activated in your sig, I found what
I believe to be zip files for linux. I don't have a linux partition, for
reasons that are OT here. Unfortunately, my middle-class existence
constrains me not to get every reference I want. MPJ


As mentioned above, you do not need Linux to view any of the files.

HTH
Abhinav

--
Nov 14 '05 #17

P: n/a
"Merrill & Michele" <be********@comcast.net> wrote:
snip...
[ Snipping is good (you should see some of the monster posts you get
without it), but please don't snip attributions for people whose text
is still in the post. For example, who wrote this: ]
Spend some time searching the standard. The last draft is
available at the location in this sig. I recommend the text
version for easy searching.

That thought had occured to me. I was dismayed to find that they want $130
for a hard copy of ANSI.


<http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470845732.html>

You don't need a copy of the Standard if you are your average C
programmer, but IMO you _do_ need one if you want to be a really good C
programmer. (Note: that's a necessary, not a sufficient condition, as
proved by the fact that I do have one.)

Richard
Nov 14 '05 #18

P: n/a
"Merrill & Michele" <be********@comcast.net> wrote:
I don't want to sound snippy, but IMO if you want to write programs
which interact with one another, you should first be rather better
acquainted with the intimacies of your OS, so that such details don't
confuse you any more. There's nothing really surprising in the
screenshot at the link you posted.
In the DOS I remember, that output would be appropriate for:
echo on
dir


Yes, that, too.
I believe what I typed should have given me either files in the root
directory or an error.
You believe incorrectly.
You weren't surprised that DOS did all the output in one window?


Not at all.

All of which is off-topic, but does prove my point, above. You can't
properly debug your C programs if you're not familiar with the
environment they interact with.

Richard
Nov 14 '05 #19

P: n/a
I believe what I typed should have given me either files in the root
directory or an error.
Richard Bos" wrote: You believe incorrectly.
You weren't surprised that DOS did all the output in one window?


Not at all.

....snipped....
I checked an older DOS and concede the point. Remarkable is not that I err,
but that I've only had two whoppers before noon today. MPJ
Nov 14 '05 #20

P: n/a
On Tue, 28 Sep 2004 14:45:10 -0500, in comp.lang.c , "Merrill & Michele"
<be********@comcast.net> wrote:
That thought had occured to me. I was dismayed to find that they want $130
for a hard copy of ANSI.


Yes but the e-copy is only $18 (pdf format, which I personally find
perfectly acceptable as I can view it on any platform you care to mention),
and you can probably print it if you want to. Its 554 pages long.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.