elementary construction +2

M

Merrill & Michele

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);
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);
}
 
K

Kenneth Brody

Merrill said:
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.
 
R

Richard Bos

Merrill & Michele said:
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);


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);
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
 
E

Eric Sosman

Merrill said:
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);


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 something that
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 to go
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!
 
M

Merrill & Michele

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
 
E

Eric Sosman

Merrill said:
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.
 
D

Default User

Merrill said:
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
 
M

Merrill & Michele

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
 
D

Default User

Merrill said:
...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
 
M

Merrill & Michele

[#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
 
C

CBFalconer

Merrill said:
...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
 
K

Kieran Simkin

Merrill & Michele said:
[#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.
 
R

Richard Bos

Merrill & Michele said:
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
 
M

Merrill & Michele

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
 
M

Merrill & Michele

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
 
C

CBFalconer

Merrill said:
snip...

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

Abhinav

Merrill said:

Text file. can be accessed through your browser

HTML again
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

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

--
 
R

Richard Bos

Merrill & Michele said:

[ 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: ]
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
 
R

Richard Bos

Merrill & Michele said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top