Gfortran, finding empty text lines?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
solarblast
Messages
146
Reaction score
2
Win 7 MinGW compilers.

I have a txt file that has empty lines that I'd like to pass by when reading them. Each line is assumed to be 85 characters. Here's a snippet of code:
Open(unit=astro_in, file="METEOR_LegacyInputToNML.txt", status = "OLD")

do while(.True.)
Read(unit=astro_in, fmt="(a85)", iostat=eof) card ! Read file card)
if ( (card(1:5) == 'GROUP') ) then
Write(*,*) "crd:",card
else if (card(1:3) == 'EOD') then
exit
else if (len(card) == 0) then
Write(*,*) "Blank", card
end if
len(card) is always 85 even if I have a line devoid of characters. len(card) is always seeing 85. How do I determine if a line is blank? Here's a few lines of the file.

GROUP Station #01 CARDS 02 fmt 501
# Station data
# ---R---|---PHIP--|---PHI---|--RLONG--|---TP----|-BN|--FL--|-STAHGT-|--COBS-|
# f10.4 f10.8 f10.8 f10.8 f10.7 f4.0 f7.3 f9.4 a8
6364.1543 .95003538 .953223371.97825229 .100000 6.203.200 00.6480MEANOK A
6364.2581 .94493380 .948132841.97141644 .100000 6.203.200 00.6721NEWBROOK B

GROUP MetTime #02 CARDS 01 fmt 502
# Time of meteor obs, no. obs per station, ... for each station
#H|MM|--SS--|----XJDM----|-NM|-NM|-NS|-NS|METNO|not input
# 2f3.0 F7.3 F13.5 I4 I4 I4 I4 A6 A6
2 12 13.0002435400.82885 40 39 1045MEANOK R2-1

GROUP SolarHz #03 CARDS 06 fmt 503

Maybe the file needs to be DAT??
 
on Phys.org
If the length is 85, it is not devoid of characters but contains 85 spaces. So this will probably suffice:
else if (card(1:10) == ' ')
 
In Fortran 90 or later, you can use the len_trim(card) to find the length without any trailing blanks.

In earler versions of fortran, just compare card with a blank striing ' '. If you compare two strings of different lengths, the shorter one is padded with blanks .

Some versions of fortran had a non-standard format descriptor Q which returned the actual number of characters read from the file, i.e.
Code:
read(astro_unit, 500) nc, card
500 format(q,a)
This was just about the only way in Fortran to find out if a line in a data file actually contains trailing blank characters - but a file format where trailing blanks are "significant" is probably a bad idea for reasons that have nothing to do with Fortran! More usefully, the Q format also tells you if the line was longer than the variable "card" and some data was truncated.
 
Ah, I've been wondering about trim and the others. I finally just put EOD at the start of the first line.