Home > Archive > Fortran > June 2005 > Printing newlines
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Jon Harrop 2005-06-02, 3:59 am |
|
I'm trying to output the header of a PGM file using one line of Fortran:
write(*,'(a, i0, a, i0, a)') 'P5\n', n, ' ', n, '\n255'
However, this is printing "\n" rather than a new line. Is it possible to
print a new line without having to split this into three separate lines of
code:
write(*,'(a2)') 'P5'
write(*,'(i0,a1,i0)') n,' ', n
write(*,'(a3)') '255'
--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com
| |
| Duane Bozarth 2005-06-02, 3:59 am |
| Jon Harrop wrote:
>
> I'm trying to output the header of a PGM file using one line of Fortran:
>
> write(*,'(a, i0, a, i0, a)') 'P5\n', n, ' ', n, '\n255'
>
> However, this is printing "\n" rather than a new line. Is it possible to
> print a new line without having to split this into three separate lines of
> code:
>
> write(*,'(a2)') 'P5'
> write(*,'(i0,a1,i0)') n,' ', n
> write(*,'(a3)') '255'
>
Try
write(*,'(a/, i0, 1x, i0/, a)') 'P5', n, n, '255'
or permutations thereof...I think that will be what I interpret you
wanting...
| |
| glen herrmannsfeldt 2005-06-02, 3:59 am |
| Jon Harrop wrote:
> I'm trying to output the header of a PGM file using one line of Fortran:
> write(*,'(a, i0, a, i0, a)') 'P5\n', n, ' ', n, '\n255'
> However, this is printing "\n" rather than a new line. Is it possible to
> print a new line without having to split this into three separate lines of
> code:
> write(*,'(a2)') 'P5'
> write(*,'(i0,a1,i0)') n,' ', n
> write(*,'(a3)') '255'
Traditionally it might be something like:
write(*,'(a/i0, 1X, i0/a)') 'P5', n, n, '255'
-- glen
| |
| Michael Gray 2005-06-02, 3:59 am |
| On Thu, 02 Jun 2005 01:40:40 +0100, Jon Harrop <usenet@jdh30.plus.com>
wrote:
>
>I'm trying to output the header of a PGM file using one line of Fortran:
>
> write(*,'(a, i0, a, i0, a)') 'P5\n', n, ' ', n, '\n255'
>
>However, this is printing "\n" rather than a new line. Is it possible to
>print a new line without having to split this into three separate lines of
>code:
>
> write(*,'(a2)') 'P5'
> write(*,'(i0,a1,i0)') n,' ', n
> write(*,'(a3)') '255'
Look up 'slash editing' in help.
write(*,'(a,/, i0, a, i0, /,a)') 'P5', n, ' ', n, '255'
will do what you intend.
| |
| Leif Harcke 2005-06-05, 3:57 pm |
| On Thu, 02 Jun 2005 01:40:40 +0100, Jon Harrop wrote:
> I'm trying to output the header of a PGM file using one line of
> Fortran:
You'll find it difficult to work with most raster image file formats
using standard Fortran I/O routines. Fortran only recently acquired
stream-oriented I/O in the 2003 revision, and this remains
unimplemented in most Fortran processors (there are processors which
have supported non-standard stream I/O for years -- but these
implementations are not portable).
Most raster formats, including TIFF, JPEG/JFIF, PBM/PGM/PPM, GIF,
etc. assume an 8-bit, bytestream-oriented I/O functionality. The
Fortran standard missed this concept by about 25 years. One exception
to the above formats is the FITS image file format used by radio
astronomers, which uses fixed-length records of 2880 bytes to
specifically be Fortran-friendly.
Stick with C or whatever your native OS uses for file I/O, and just
write wrappers to call these routines from Fortran. If you insist on
using Fortran, some F77 code which uses a direct access file with a
record length of 1 to simulate bytestream output appears below. You
don't really want to do this, though. The behavior of direct access
files isn't standardized, and doing direct-access with single byte
records is *really* slow.
-Leif
------------------------------
program test
integer rec_ct ! global pointer into bytestream
common/counter/rec_ct
character*1024 header ! internal file for forming strings
integer nx,ny ! dimensions of raster
write(*,*) 'Writing header file...'
open(20,file='pgm.hdr',access='direct',recl=1,iostat=ios)
if (ios .ne. 0) then
write(*,*) 'Error opening output file, iostat: ',ios
stop
end if
rec_ct = 1
call writeln('P5')
call writeln('# PGM generated by test program')
c use an internal file to construct the string
write(header,'(I4,A1,I4)') nx,' ',ny
c now write the formed string into the bytestream
call writeln(header)
call writeln('255')
close(20)
end
subroutine writeln(str)
c write a newline terminated string to output unit #20
character*(*) str
integer rec_ct,i
integer strlen
common/counter/rec_ct
c write(*,*) 'strlen: ',strlen(str)
do i=1,strlen(str)
c write(*,*) 'rec_ct: ',rec_ct
write(20,rec=rec_ct) str(i:i)
rec_ct = rec_ct + 1
end do
write(20,rec=rec_ct) char(10)
rec_ct = rec_ct + 1
return
end
| |
| Rich Townsend 2005-06-05, 8:57 pm |
| Leif Harcke wrote:
> On Thu, 02 Jun 2005 01:40:40 +0100, Jon Harrop wrote:
>
>
>
> You'll find it difficult to work with most raster image file formats
> using standard Fortran I/O routines. Fortran only recently acquired
> stream-oriented I/O in the 2003 revision, and this remains
> unimplemented in most Fortran processors (there are processors which
> have supported non-standard stream I/O for years -- but these
> implementations are not portable).
>
> Most raster formats, including TIFF, JPEG/JFIF, PBM/PGM/PPM, GIF,
> etc. assume an 8-bit, bytestream-oriented I/O functionality. The
> Fortran standard missed this concept by about 25 years. One exception
> to the above formats is the FITS image file format used by radio
> astronomers, which uses fixed-length records of 2880 bytes to
> specifically be Fortran-friendly.
>
> Stick with C or whatever your native OS uses for file I/O, and just
> write wrappers to call these routines from Fortran. If you insist on
> using Fortran, some F77 code which uses a direct access file with a
> record length of 1 to simulate bytestream output appears below. You
> don't really want to do this, though. The behavior of direct access
> files isn't standardized, and doing direct-access with single byte
> records is *really* slow.
I agree with most of your general remarks; but IIRC P*M is an exception
to the 'binary' stream I/O requirement, in that it supports file formats
where images are stored as ASCII (there is also a 'RAW' binary format).
I've had no problems writing such images from FORTRAN. Of course, they
can take up a huge amount of space; but if they are immediately
converted to some other format (using one of the useful tools in the
netpbm package), then that doesn't create a problem.
More details of the ASCII file format can be found here:
http://www.daubnet.com/formats/PBM.html
cheers,
Rich
|
|
|
|
|