
/*
 * exercise in POSIX, strings, character
 * conversion and arrays.
 *
 * burton rosenberg
 * march 2005
 *
 */

#include<stdio.h>   /* fopen, fgets, fclose */
#include<string.h>  /* strtok */
#include<stdlib.h>  /* atoi */

#define USAGE "usage: %s filename filename\n"
#define BUFFER_SIZE 1024
#define DELIM " \t\n" 

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

   FILE * fin, * fout ;
   char line[BUFFER_SIZE] ;
   char * s ;

   if ( argc != 3 ) {
      printf( USAGE, argv[0] ) ;
      exit(0) ;
   }

   fin = fopen( argv[1], "r" ) ;
   if ( fin==NULL ) {
      printf("error: cannot open file %s for read\n", argv[1] ) ;
      exit(1)  ;
   }
   fout = fopen( argv[2], "w" ) ;
   if ( fout==NULL ) {
      printf("error: cannot open file %s for write\n", argv[2] ) ;
      exit(1)  ;
   }

   while ( fgets(line, BUFFER_SIZE, fin) ) {
      s = strtok(line, DELIM) ;
      while ( s ) {
        fprintf(fout, "%d\n", atoi(s) ) ;
        s = strtok(NULL, DELIM) ;
      }
   }

   fclose(fin) ;
   fclose(fout) ;

}


