Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Including blank lines when reading a file.

Status
Not open for further replies.

jeansberg

Programmer
Oct 5, 2006
7
FI
Hello

I am learning Fortran and I am currently trying to write a program that reads a text-file and prints it on the screen. The problem I am having is that all blank lines are skipped.

I get this on the screen:

****.123
******.abc
****.txt

Instead of this:

****.123

******.abc

****.txt


Here is my program for reference.
---------------------------------------------------
Program quad

integer i, j
character a*30

OPEN (4, FILE='test.txt', STATUS='old')

do 20

read(4,*, end=10) a
write(*,*) a
a=''

20 continue

10 stop
end
---------------------------------------------------

Any tips are appreciated!
 
Hello

Try this:

Program quad
character*30 a
open (4,file='test.txt',status='old')
do while(.true.)
read(4,'(a)',end=10) a
if(a.eq."") a = " "
write(*,'(1x,a)') a
enddo
10 continue
end
 
Thank you very much, gullipe! It works as it should now.

I have to ask you what the meaning of '(a)' is in the following line:
----------------------
read(4,'(a)',end=10) a
----------------------

I think it is some kind of formatting, but I haven't studied that very much yet.
 
Hello

Yes, the "a" is an edit descriptor for character strings.

If you are reading integers, you write:
read (4,'(i10,i10,i10)') i,j,k
or just:
read (4,'(3i10)') i,j,k

If you are reading real variables, you write:
read (4,'(3f10.0)') a,b,c

If you want to WRITE these variable to a FILE with THREE decimals, you write:
write (3,'(3f10.3)') a,b,c

If you want to WRITE these variable on the SCREEN with three decimals, you write:
write (*,'(1x,3f10.3)') a,b,c

If you are reading a character variable, you write:
read (4,'(a)') a

If you want to WRITE this character string on the SCREEN, you better use the edit descriptor '1x' also, which outputs one blank character first:
write (3,'(1x,a)') a
or even better:
i = len_trim(a) ! i is then the length of a
write (3,'(1x,a)') a(1:i)

In the old days the first character (or letter) written on the screen or on printers was a kind of control character: "0" or "+" meant extra line if I remember correctly and "1" meant a page brake. This may still extist in some compilers, so always use '1x' when you write on screen or directly on a printer. You can skip '1x' when you write to a file.

Best wishes
gullipe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top