Gfortran, finding empty text lines?

In summary: I also think I used a different unit than astro_in, might have been astro_out.In summary, the txt file has empty lines that Win 7 MinGW compilers are not able to read.
  • #1
solarblast
152
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
  • #2
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) == ' ')
 
  • #3
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.
 
  • #4
Ah, I've been wondering about trim and the others. I finally just put EOD at the start of the first line.
 
  • #5


Thank you for reaching out with your question about Gfortran and finding empty text lines. Based on the code snippet and file sample provided, it seems like the issue may be related to the format specifier used in the Read statement. Since the length of the card being read is always 85 characters, it may be helpful to try using a different format specifier that takes into account the possibility of blank lines.

For example, you could try using "(a)" as the format specifier instead of "(a85)". This will read in the entire line, regardless of its length. Then, you can use the Len_trim function to determine the actual length of the line (excluding any trailing blank characters). If the length is zero, then the line is considered blank.

Alternatively, you could also try using the "iostat" variable in the Read statement to check for any errors or end-of-file conditions. If the value of "iostat" is not equal to zero after the Read statement, then you can assume that the line is blank.

Additionally, it may be helpful to check if the file is in the correct format (DAT vs TXT) and if there are any other issues with the file that could be causing the unexpected behavior.

I hope this helps and good luck with your project!
 

1. How do I use Gfortran to find empty text lines in a file?

To use Gfortran to find empty text lines in a file, you can use the READ statement to read each line of the file and the LEN_TRIM function to check if the line has any content. If the length of the trimmed line is zero, then it is an empty line.

2. Can Gfortran identify and remove empty text lines from a file?

Yes, Gfortran can identify and remove empty text lines from a file. After using the READ statement to read each line of the file, you can use the IF statement to check if the line is empty, and if it is, you can use the DELETE statement to remove it from the file.

3. Is there a way to count the number of empty text lines in a file using Gfortran?

Yes, there is a way to count the number of empty text lines in a file using Gfortran. You can use a counter variable and increment it every time an empty line is encountered while reading the file. The value of the counter at the end will be the total number of empty lines in the file.

4. Can Gfortran handle files with a large number of empty text lines?

Yes, Gfortran can handle files with a large number of empty text lines. Since Gfortran reads and processes each line of the file individually, the number of empty lines in the file does not affect its performance.

5. Are there any other built-in functions in Gfortran that can help with identifying empty text lines?

Yes, Gfortran has other built-in functions that can help with identifying empty text lines. For example, the LEN function can be used to check the length of a string, and the INDEX function can be used to check if a string contains any characters. These functions can be used in combination to identify and handle empty text lines in a file.

Similar threads

  • Programming and Computer Science
Replies
4
Views
6K
Back
Top