[comp.lang.fortran] Writing newlines to strings

ems@aristotle.JPL.NASA.gov (Eric Slimko) (11/30/90)

I'd like to write a newline character to a string using a 
FORMAT statement.  Unfortunately, while I can write them to
the screen, I can't seem to find a way to write them to a string.
The code fragement I am working with is like this:

        CHARACTER*512   ERRMSG

        WRITE(ERRMSG,1001) 0.7733383D0, -0.5141431981D0
1001    FORMAT(/'ERROR IN READING PLANETARY EPHEMERIS FILE --',
     *              'REQUESTED TIME TOO EARLY'/
     *              '   REQUESTED  TIME = ',D25.15/
     *              '   FILE START TIME = ',D25.15)


The forward slash (/) is a kind of end-of-record marker in Fortran and
will cause an error when written to a string.  I have used the C-type
newline (\n) within the string and had it work for Sun Fortran, but that
is not portable, which it must be.  Is there a standard that I'm missing?
I'd appreciate E-Mail if you could, because I'm an estranged C-Programmer
and don't read this newgroup often. :-)

-- 
Eric Slimko                |  Jet Propulsion Laboratories
ems@aristotle.jpl.nasa.gov |  NASA/CalTech

br0w+@andrew.cmu.edu (Bruno W. Repetto) (11/30/90)

Do the following changes to your code:

1) Add char*2 variable 'crlf'.
2) Give 'crlf' the value char(13)+char(10) (carriage-return+line-feed)
3) Change all '/' in your format to 'a2'
4) Include 'crlf' in the appropriate places in the list of variables to write,
   so that they match with the 'a2' format spec.

So the code now reads:

        character errmsg*512,crlf*2
        crlf=char(13)//char(10)
        WRITE(ERRMSG,1001)crlf,crlf,
     *                    0.7733383D0,crlf,-0.5141431981D0,crlf
1001    FORMAT(a2,'ERROR IN READING PLANETARY EPHEMERIS FILE --',
     *              'REQUESTED TIME TOO EARLY',a2,
     *              '   REQUESTED  TIME = ',D25.15,a2,
     *              '   FILE START TIME = ',D25.15,a2)

        print *,errmsg
        end

Using f77 on a Sun4/330 (which runs SunOS 4.1), the output is:

ERROR IN READING PLANETARY EPHEMERIS FILE --REQUESTED TIME TOO EARLY
   REQUESTED  TIME =      .773338300000000D+00
   FILE START TIME =     -.514143198100000D+00
                                                                     
                                                                               
                                                                               
                                                                               
                                      
There are extra blank lines because the 512-char variable errmsg is too long.
You may run into aesthetic problems because of this.  Maybe a shorter variable
will do.  Or if you change:

	print *,errmsg

to read:

	print *,(errmsg(i:i),i=1,length)

where 'length' is a variable you compute before printing, you can get rid of
the blank spaces.

Hope this helps! Bruno. br0w+@andrew.cmu.edu

ems@aristotle.JPL.NASA.gov (Eric Slimko) (12/01/90)

Wow!  The people here in comp.lang.fortran are quite interested
in helping their fellow programmers!  I never had that many responses
to a posted question before.

Well, thanks to those that responded!

And if your interested, the best result for me ended up using an ARRAY
of strings to store multiple "lines," rather then wring newlines to strings.
There were some suggestions about writting newlines to strings, but while they
typically worked on Suns, they only partially worked on VMS.  

Thanks again, it was appreciated!
-- 
Eric Slimko                |  Jet Propulsion Laboratories
ems@aristotle.jpl.nasa.gov |  NASA/CalTech