C in awk script 
Author Message
 C in awk script

hello,
I want to program C code in awk script (with an interface).
I would like if it's possible or not ?
Thanks.
Philippe Barabinot


Mon, 10 Feb 2003 03:00:00 GMT  
 C in awk script


Quote:
>hello,
>I want to program C code in awk script (with an interface).
>I would like if it's possible or not ?
>Thanks.
>Philippe Barabinot

Suppose this is the C program you want to run:-

#include <stdio.h>

int main()
{
  FILE *fp;
  float x,y;
  if ( (fp = fopen("cfromawk.dat","r")) == NULL ) {
    printf("%s\n","error opening cfromawk.dat for input");
    exit(1);
  }
  while ( fscanf(fp,"%f",&x) == 1 ) {
    y = x*x;
    printf("%.8g\n",y);
  }
  fclose(fp);

Quote:
}

From awk, you can write the program, compile it, give it some data, run
it, and receive the results something like this:-

BEGIN {

  basename="cfromawk"

  #write the text of a C program to a file
  #note \\n instead of \n and \" instead of "
  cfile=basename".c"
  print "#include <stdio.h>" > cfile
  print "" > cfile
  print "int main()" > cfile
  print "{" > cfile
  print "  FILE *fp;" > cfile
  print "  float x,y;" > cfile
  print "  if ( (fp = fopen(\"cfromawk.dat\",\"r\")) == NULL ) {" >
cfile
  print "    printf(\"%s\\n\",\"error opening cfromawk.dat for
input\");" > cfile
  print "    exit(1);" > cfile
  print "  }" > cfile
  print "  while ( fscanf(fp,\"%f\",&x) == 1 ) {" > cfile
  print "    y = x*x;" > cfile
  print "    printf(\"%.8g\\n\",y);" > cfile
  print "  }" > cfile
  print "  fclose(fp);" > cfile
  print "}" > cfile
  close(cfile)

  #compile the program
  exefile=basename".exe"
  system("rm -f " exefile)
  system("gcc -o " exefile " " cfile)
  cmd="ls "exefile
  cmd|getline
  close(cmd)
  if ($0!~exefile) {
    print "compilation failed"
    exit
  }

  #write some data for input to the C program
  datfile=basename".dat"
  system("rm -f " datfile)
  print "3.5" > datfile
  print "-1.85e-3" > datfile
  close(datfile)

  #run the program and receive its output
  while (exefile|getline==1) print $0
  close(exefile)

Quote:
}

I have tested this on my system using Windows 95, DJGPP gcc, gawk3.0.6
and some DJGPP Unix-like utilities including rm and ls. I think it
should work "as is" on Unix systems but I am not in a position to test
that.

C:\temp>gawk -f cfromawk.awk
12.25
3.4225e-06

C:\temp>ls -l cfromawk*
-rw-r--r--   1 dosuser  root         1284 Aug 26 12:48 cfromawk.awk
-rw-r--r--   1 dosuser  root          297 Aug 26 12:56 cfromawk.c
-rw-r--r--   1 dosuser  root           15 Aug 26 12:56 cfromawk.dat
-rwxr-xr-x   1 dosuser  root       114650 Aug 26 12:56 cfromawk.exe

Does this meet your needs?
--
Alan Linton



Wed, 12 Feb 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Access to Script Name Within Awk Script

2. Creating an awk script to extract other scripts from a file

3. to CS: or not to CS: in F-PC assembler

4. Replacing a string from an input file within awk script

5. Awk-Script for Text Analysis

6. piping input files into awk script

7. problems with awk script

8. I want to put my awk file into ksh script

9. name of awk script

10. passing arguments from awk to another script

11. cgi awk script

12. awk script error

 

 
Powered by phpBB® Forum Software