Quantcast
Channel: Intel® Software - Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2746

Behavior of FPUTC() with newline

$
0
0

Howdy.  One more trip into Legacyland.  I have reduced my issue to a small test case that works with "ye olde" g77 compiler, but does not work with Intel Fortran, even if I "USE IFPORT" to invoke the portability version of FPUTC().

The following code when compiled with g77 (apologies for the ancient style and features like equivalencing) produces a file that consists of two lines:

hi
there

      PROGRAM OPENTEST

      INTEGER RESULT,IOSTAT

      INTEGER*1 INEWLINE

      CHARACTER*1 CNEWLINE

      EQUIVALENCE(INEWLINE,CNEWLINE)

      INEWLINE=10 

      OPEN(UNIT=14,STATUS='UNKNOWN',FILE='OUT.TXT',

     *ACCESS='SEQUENTIAL',FORM='FORMATTED',RECL=512,

     *BLANK='NULL',IOSTAT=IOSTAT)

      RESULT=FPUTC(14,'h')

      RESULT=FPUTC(14,'i')

      RESULT=FPUTC(14,CNEWLINE)

      RESULT=FPUTC(14,'t')

      RESULT=FPUTC(14,'h')

      RESULT=FPUTC(14,'e')

      RESULT=FPUTC(14,'r')

      RESULT=FPUTC(14,'e')

      RESULT=FPUTC(14,CNEWLINE)

      END

 

Yup, it should not be that hard, but I am trying to demonstrate how the legacy code works with a trivial test case.  Note the use of FPUTC, which I am told is a g77 intrinsic function and accepts a logical unit number as the first argument.  Also notice that for the newline character, I am storing 10 in a 1-byte integer, and equivalencing it with a character.  I plan to modernize this 40-year-old application, but I first need to get the program working on Intel Fortran to have a starting point for modernization.  (These are the steps that our sponsor finds most acceptable.)

Aside, not my issue:  The program as coded above would crash on Intel Fortran when FPUTC is invoked, presumably because it is invoking the C version, which expects a pointer to a file descriptor as the first argument.  But that is easily solved by using IFPORT:

      PROGRAM OPENTEST
      USE IFPORT

      INTEGER RESULT,IOSTAT

      INTEGER*1 INEWLINE

      CHARACTER*1 CNEWLINE

      EQUIVALENCE(INEWLINE,CNEWLINE)

      INEWLINE=10 

      OPEN(UNIT=14,STATUS='UNKNOWN',FILE='OUT.TXT',

     *ACCESS='SEQUENTIAL',FORM='FORMATTED',RECL=512,

     *BLANK='NULL',IOSTAT=IOSTAT)

      RESULT=FPUTC(14,'h')

      RESULT=FPUTC(14,'i')

      RESULT=FPUTC(14,CNEWLINE)

      RESULT=FPUTC(14,'t')

      RESULT=FPUTC(14,'h')

      RESULT=FPUTC(14,'e')

      RESULT=FPUTC(14,'r')

      RESULT=FPUTC(14,'e')

      RESULT=FPUTC(14,CNEWLINE)

      END

 

This program results in an empty file.  I also tested with a single record, without outputing CNEWLINE, and actually saw the characters in the output file.  So this code basically works, but writing ASCII 10 obliterates each line.

 

My compile line is:  ifort -c -debug -save -assume nounderscore

 

(The application requires -save because it assumes that local variables retain their values between calls; that's just the way it is.)

 

Basically, in a program like this, how should I start a new line?

 

JayB


Viewing all articles
Browse latest Browse all 2746

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>