Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a c-programm 1

Status
Not open for further replies.

frapp

Technical User
Apr 25, 2005
3
DE
Hi,
does someone know how to call a c-programm(from within a rexx program) and pass a stem-variable or array as input parameter? And maybe a short example how the c-program deals with stems/arrays and returns them back(to the rexx program)?
 
Code:
#include <stdio.h>
int main ( int argc, char *argv[] ) {
  int i;
  for ( i = 0 ; i < argc ; i++ ) {
    printf( "Arg %d = %s\n", i, argv[i] );
  }
  return 3;
}

When run from a command line, this is what you get
Code:
$ gcc prog.c

# run the program with some command line parameters
$ ./a.out hello world, how are you today?
Arg 0 = ./a.out
Arg 1 = hello
Arg 2 = world,
Arg 3 = how
Arg 4 = are
Arg 5 = you
Arg 6 = today?

# This is the exit status of the program (the return 3; at the end of main)
# use this to pass back basic success/fail status information.
$ echo $?
3
This simple program reads its parameters from the command line.

Another way is to get the program to read from input and write to output (does rexx have the same idea as pipes in Unix/Linux?)
Code:
#include <stdio.h>
int main ( int argc, char *argv[] ) {
  char buff[BUFSIZ];
  while ( fgets ( buff, BUFSIZ, stdin ) != NULL ) {
    fputs ( buff, stdout );
  }
  return 0;
}

This can be invoked in a number of ways
Code:
$ gcc hello.c

# read from a file, output to terminal
$ ./a.out < hello.c
#include <stdio.h>
int main ( int argc, char *argv[] ) {
  char buff[BUFSIZ];
  while ( fgets ( buff, BUFSIZ, stdin ) != NULL ) {
    fputs ( buff, stdout );
  }
  return 0;
}

# read from a file, write to another file
$ ./a.out < hello.c > results.txt

# read from a file, send output to another program
$ ./a.out < hello.c | wc
      8      34     176

Experiment with both those forms (which just print back at you what you pass in) to see which works best for what you've got.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top