[comp.os.vms] FORTRAN DOUBLE SPACES PRINTED LINES WITH TABS

graham@DRCVAX.ARPA (03/18/88)

Hi All,

Perhaps this is a novice question but...

I have a FORTRAN program that opens a text file and prints portions of it 
to the screen.  It seems that any line which contains a tab is double 
spaced by Fortran.  If I try to print a portion of a program, which uses 
tabs in every line, the complete screen is double spaced, one line of text, 
one blank line, etc.  Here are the open, print and format statements, 
perhaps one of you can tell me what I am doing wrong.

	OPEN(UNIT=1,TYPE='OLD',NAME=FILE,READONLY,CARRIAGECONTROL='LIST')
	.
	PRINT 20,LINE
20	FORMAT(1X,A80)

If one of you could tell me why this inserts a CRLF after any line 
containing a tab, I would be much obliged.

Dan
graham@drcvax.arpa
------

carl@CITHEX.CALTECH.EDU (Carl J Lydick) (03/20/88)

 > I have a FORTRAN program that opens a text file and prints portions of it 
 > to the screen.  It seems that any line which contains a tab is double 
 > spaced by Fortran.  If I try to print a portion of a program, which uses 
 > tabs in every line, the complete screen is double spaced, one line of text, 
 > one blank line, etc.  Here are the open, print and format statements, 
 > perhaps one of you can tell me what I am doing wrong.
 > 
 > 	OPEN(UNIT=1,TYPE='OLD',NAME=FILE,READONLY,CARRIAGECONTROL='LIST')
 > 	.
 > 	PRINT 20,LINE
 > 20	FORMAT(1X,A80)
 > 
 > If one of you could tell me why this inserts a CRLF after any line 
 > containing a tab, I would be much obliged.

I can't be sure (you've elided the part of the code that would tell me), but I
suspect  that you've declared LINE to be CHARACTER*80.  If so, that means that
your PRINT statement is going to print 80  characters  (LINE  will  have  been
padded  to  80 characters with blanks if the input line was shorter).  Since a
TAB is a single character, but acts like 8  spaces,  you're  trying  to  print
something  that looks to the terminal like an 87-character line.  The line is,
therefore, not double-spaced, but wrapped.

art@ccelsn (Art McClinton (703)883-6356) (03/21/88)

I think that your problem stems from the fact that LINE is defined
as  CHARACTER*80.  Thus it is blank filled on the right.  A TAB is
expanded to more than one blank but only fills one space in the array.

The OPEN (UNIT=1... has nothing to do with this problem as the PRINT
is not using it.  You want to trim trailing blanks before you print.
I suggest somthing like:
	CHARACTER*1 BLANK/' '/
	
	DO LAST=80,1,-1
	IF (LINE(LAST:LAST) .NE. BLANK) GO TO 100
	END DO
	LAST=1
100	PRINT 20,LINE(1:LAST)
20	FORMAT(1X,A)

Another way would be to determine the length during the READ.

FOR EXAMPLE.

	READ(5,21,...) LENGTH,LINE(1:LENGTH)
21	FORMAT(Q,A)

will result in LENGTH being set equal to the length of the array and
the line is then read into the Character variable LINE.
s*
*---Art
*
*Arthur T. McClinton Jr.     ARPA: art@mitre.arpa
*Mitre Corporation           Phone: 703-883-6356
*7525 Colshire Drive         Internal Mitre: ART@MWVMS or M10319@MWVM
*McLean, Va. 22102-3481      DCS: MCCLINTON
*
* I apologize for the presently non-replyable return addresses.  Hope to
* fix that soon.

nagy%warner.hepnet@LBL.GOV (Frank J. Nagy, VAX Wizard & Guru) (03/22/88)

>     OPEN(UNIT=1,TYPE='OLD',NAME=FILE,READONLY,CARRIAGECONTROL='LIST')
>     .
>     PRINT 20,LINE
> 20    FORMAT(1X,A80)
>      
> If one of you could tell me why this inserts a CRLF after any line
> containing a tab, I would be much obliged.
     
I think that what you are seeing is line wrapping.  Lines are read
from the file into variable line; standard FORTRAN practice pads LINE
to 80 characters with blanks.  If there are no tabs in LINE, then your
PRINT statement will print 81 characters with the first being swallowed
for carriage control and the next 80 filling a line on the display.  If
LINE contains any tabs (and each tab is a single character to the FORTRAN
program), then the terminal driver expands the tabs, the end of the line
(remember, LINE has a full 80 characters) goes over the right edge of the
screen and you get a line wrap (which is a normal setting for terminals).
If you do SET TERMINAL/NOWRAP and run your program, the double spacing
should disappear.

Alternatively, you can modify your program to get the size of LINE as
read (see the "Q" format information in the FORTRAN manual - section
12.2.12.1 on page 12-24 of the "Programming in VAX FORTRAN" manual).
Then, with the length of LINE in LINELEN, your PRINT and FORMAT statements
become:

	PRINT 20,LINE(1:LINELEN)
20	FORMAT(1X,A)



= Frank J. Nagy   "VAX Guru & Wizard"
= Fermilab Research Division EED/Controls
= HEPNET: WARNER::NAGY (43198::NAGY) or FNAL::NAGY (43009::NAGY)
= BitNet: NAGY@FNAL
= USnail: Fermilab POB 500 MS/220 Batavia, IL 60510

jeh@crash.cts.com (Jamie Hanrahan) (03/23/88)

In article <8803211520.AA00743@ucbvax.Berkeley.EDU> <graham@drcvax.arpa> writes:
>	OPEN(UNIT=1,TYPE='OLD',NAME=FILE,READONLY,CARRIAGECONTROL='LIST')
>	.
>	PRINT 20,LINE
>20	FORMAT(1X,A80)
>
>If one of you could tell me why this inserts a CRLF after any line 
>containing a tab, I would be much obliged.
>
>Dan
>graham@drcvax.arpa


The trouble is that, after the carriage control byte (1X) is removed by the
terminal driver, you're always sending 80 characters, regardless of the length
of the record you get from the input file.  When one (or more) of these is a
tab, the cursor moves more than one column for one (or more) of those 80 
characters, so the cursor moves right more than 80 columns; if the terminal
is set to wraparound, it injects the CRLF.  

If you want to fix it in the "environment", turn the terminal's wraparound
feature off, and do a SET TERM /NOWRAP .  To fix it in the code, read your
record with something like

	READ (lun, 10) LINE_LENGTH, LINE
10	FORMAT (Q, A)

and write it with 

	PRINT 20, LINE(1:LINE_LENGTH)
20	FORMAT (1X, A)

The "Q" format code returns the "number of characters remaining to be
transferred in an input record", so the integer variable LINE_LENGTH
gets the actual record length.   Then you write out just that substring,
avoiding the trailing blanks that Fortran wants to give you.  The "A"
format code, used without a field-width specifier on a character variable
or expression, defaults to the actual length of the expression.  Uhhh...
you'll probably have to do something like 

	LINE_LENGTH = MAX (LINE_LENGTH, 1)

to handle zero-length records, as Fortran abhors zero-length strings. 

BTW, the CARRIAGECONTROL="LIST" does absolutely nothing for you on input
files, except possibly avoid a record attribute mismatch warning-level 
message.  On output files to disk it merely specifies what value to store 
in the record attribute field in the file header, and has no effect on the
data records written to disk.  On output files to carriage-control devices
like terminals and printers, it does what you think it will.  So if instead
of using PRINTs you WRITE your output to a lun opened with CARRIAGECONTROL=
"LIST", you can get rid of the "1X" fortranism in your output formats.  

chpf127@ut-emx.UUCP (J. Eaton) (03/23/88)

In article <8803211520.AA00743@ucbvax.Berkeley.EDU>, graham@DRCVAX.ARPA writes:
> 
> 	OPEN(UNIT=1,TYPE='OLD',NAME=FILE,READONLY,CARRIAGECONTROL='LIST')
> 	.
> 	PRINT 20,LINE
> 20	FORMAT(1X,A80)
> 
> If one of you could tell me why this inserts a CRLF after any line 
> containing a tab, I would be much obliged.
> 
> Dan
> graham@drcvax.arpa
> ------

It's not inserting a CRLF after the line.  It's wrapping because you've
opened the file with carriage control='list', but have used the format
(1x,a80), which prints 81 characters due to the 1x.  The first column
in your file is also blank because of the 1x.

Solutions:

  1. Open the file without the nonstandard carriage control qualifier

     pro:  this is standard fortran
     con:  other programs may do funny things to your file since it will
           have fortran type carriage control (i.e. the first column of
           data is interpreted as a carriage control character, hence
           the need for the 1x in the format)

  2. drop the 1x in the format

     pro:  you will be printing only 80 characters and the lines won't
           wrap.  the file will be opened with the carriage control
           characteristics you want.
     con:  this is not standard fortran



J. Eaton
Department of Chemical Engineering
The University of Texas at Austin
Austin, Texas  78712

I've written my fair share of 'em, but still, after all this time,
I've never met a FORTRAN program I really liked.