View previous topic :: View next topic |
Author |
Message |
skeptic
Joined: 22 Mar 2009 Posts: 14
|
Posted: Wed Aug 27, 2014 10:31 am Post subject: outputting a blank line |
|
|
I have a program that reads a text file line by line, making occasional adjustments, then outputting the (revised) line character by character.
This works fine except for completely blank lines. Using FMT="(/)" or WRITE(7,*) with no output list produces a single space character, not a totally blank line. |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Wed Aug 27, 2014 11:46 am Post subject: |
|
|
Do you use lines of a fixed / constant length? Then you may do the following:
Code: | character*80 blankline ! set 80 to the line length you need
blankline = char(32)
write(7,'(A80)')blankline |
Hope this works
Wilfried |
|
Back to top |
|
 |
skeptic
Joined: 22 Mar 2009 Posts: 14
|
Posted: Wed Aug 27, 2014 12:00 pm Post subject: |
|
|
I can reproduce lines consisting of n space characters, but not lines containing nothing at all. |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Wed Aug 27, 2014 12:33 pm Post subject: |
|
|
If you output via a list directed * format, then it will always output a space at the beginning of the line due to the old line printer codes where the frist character being a space meant print on next line, a + for overtype and a 1 for form feed.
try this:
Code: |
character*80 blankline
blankline = ' '
write(7,'(A)')trim(blankline)
|
This avoids trailing spaces including a single space at the beginning of a blank line. |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Wed Aug 27, 2014 2:39 pm Post subject: |
|
|
Ian,
My version of your code produced a single blank. Code: | character*80 blankline
integer*4 i
!
open (11,file='blank.txt')
!
blankline = ' '
!
do i = 1,5
write (11,'(A)') trim (blankline)
end do
write (11,11)
11 format (//'test line'/' '//)
end |
The only way I can definitely do it is to open a file as TRANSPARENT. The following example appears to work. Code: | character*80 blankline
integer*4 i
!
open (11,file='blank.txt',ACCESS='TRANSPARENT', FORM='FORMATTED')
!
blankline = ' '
!
do i = 1,5
write (11,'(A/)') trim (blankline)
end do
write (11,11)
11 format (//'test line'/' '//)
end |
John |
|
Back to top |
|
 |
skeptic
Joined: 22 Mar 2009 Posts: 14
|
Posted: Thu Aug 28, 2014 11:26 am Post subject: |
|
|
Many thanks, John. It works exactly as you stated. The only drawback is, I believe, that TRANSPARENT is not a standard Fortran feature? However, it produces exactly what I needed. |
|
Back to top |
|
 |
simon
Joined: 05 Jul 2006 Posts: 299
|
Posted: Thu Aug 28, 2014 5:31 pm Post subject: |
|
|
You could use ACCESS='STREAM' instead of TRANSPARENT. It does not appear to be available in FTN95, but is a F2003 standard, I believe. |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Mon Sep 01, 2014 7:22 pm Post subject: |
|
|
The way to print a new line to an output unit that is opened for writing formatted data is to use an empty format specifier with an empty data list, that is:
This is fully standard compliant. No need for anything fancy or to jump though hoops to do this. _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Tue Sep 02, 2014 12:13 am Post subject: |
|
|
david,
write (10,'()') produces a single space character, not a totally blank line. |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Tue Sep 02, 2014 6:59 am Post subject: |
|
|
Yes, FTN95 produces a record containing a blank character.
But this is possibly a bug in the FTN95 compiler; the standard mandates that an empty record should be written. It is permissible for the compiler to output a blank if page control is being used, but this should only be done when list-directed output is being used.
A related bug is that the following should produce 2 empty records but produces two records containing a blank character.
Code: |
program writeblank
open(10,file='result.txt')
write(10,'("Line before blank lines")')
! This should produce two blank lines but doesn't
write(10,'(/)')
write(10,'("Line after blank lines")')
close(10)
end program writeblank
|
_________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8208 Location: Salford, UK
|
Posted: Wed Jan 28, 2015 2:19 pm Post subject: |
|
|
This bug has now been fixed for the next release. |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Jan 28, 2015 9:07 pm Post subject: |
|
|
Wow. I had forgotten about this one.
Thanks Paul. Lots of good changes coming in the next release I see  _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
|