Gfortran, finding empty text lines?

Click For Summary

Discussion Overview

The discussion revolves around reading a text file in Fortran while handling empty lines. Participants explore methods to identify and skip lines that may appear empty but still have a fixed length of 85 characters.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant notes that the length of 85 indicates the line is not devoid of characters but may contain spaces, suggesting a comparison to a string of spaces to identify blank lines.
  • Another participant mentions using the `len_trim` function in Fortran 90 or later to find the length of the string without trailing blanks, contrasting it with earlier versions where a blank string comparison could be used.
  • A participant references a non-standard format descriptor 'Q' that could return the actual number of characters read, which could help determine if a line contains trailing blanks.
  • One participant indicates a workaround by placing 'EOD' at the start of the first line to avoid the issue altogether.

Areas of Agreement / Disagreement

Participants express differing views on how to handle the detection of empty lines, with no consensus on a single method being established. Various approaches are proposed, but the discussion remains unresolved regarding the best solution.

Contextual Notes

Some limitations include the dependence on Fortran version differences and the potential issues with file formats that treat trailing blanks as significant.

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??
 
Technology news 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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
7K