Fortran 90 how to read how many characters are in a line?

  • Context: Fortran 
  • Thread starter Thread starter hodei_cloud
  • Start date Start date
  • Tags Tags
    Fortran Line
Click For Summary

Discussion Overview

The discussion revolves around how to count the number of characters in each line of a text file using Fortran 90. Participants explore various methods and considerations related to file input/output in Fortran, including standard and non-standard approaches.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant shares a code snippet for counting lines but seeks guidance on counting characters within each line.
  • Another participant suggests using a loop to count characters and mentions the importance of recognizing end-of-line characters, which vary by operating system.
  • A different participant argues that Fortran's sequential file I/O is line-based, making it difficult to access end-of-line characters directly, and proposes using direct access file methods instead.
  • There is mention of non-standard methods and external utilities, such as using C subroutines or Unix utilities like awk or perl for preprocessing the file.
  • One participant expresses uncertainty about the complexity of raw input/output and considers simpler alternatives.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to count characters in a line, with multiple competing views and methods discussed.

Contextual Notes

Participants highlight limitations in their knowledge of Fortran's capabilities, particularly regarding non-standard methods and the handling of end-of-line characters. There is also mention of the absence of certain subroutines in standard textbooks.

hodei_cloud
Messages
19
Reaction score
0
Hello everyone, this is the thing: (Before anything I have to say that I trying to make the program in fortran 90)

I have a text to read, and in that text i have to know how many characters are in every single line. I make it to count how many lines are in the text with this code:

n=0
do
read(unit=111,fmt="(1a)",iostat=s) line
if (s<0) then
exit
endif
n=n+1
enddo

But I have no similar idea to count the characters within a line! I tried to search for it, but no result.
Thank you for your help!
 
Technology news on Phys.org
To make your code more readable, and to preserve your indentation, put a [ code ] tag at the top and a [ /code ] tag at the bottom (both without extra spaces). I have done that in your code.
hodei_cloud said:
Hello everyone, this is the thing: (Before anything I have to say that I trying to make the program in fortran 90)

I have a text to read, and in that text i have to know how many characters are in every single line. I make it to count how many lines are in the text with this code:
Code:
n=0
do
  read(unit=111,fmt="(1a)",iostat=s) line
  if (s<0) then
     exit
  endif
n=n+1
enddo
But I have no similar idea to count the characters within a line! I tried to search for it, but no result.
Thank you for your help!
Depending on whether you are using Linux or Windows, the operating system uses one or two characters to mark the end of a line. In Linux it's one character (with ASCII code 13 I think) and in Windows, the characters with ASCII codes of 13 and 10 are used. ASCII 13 is CR (carriage return) and ASCII 10 is LF (line feed).

After reading in a line, use a loop of some kind to look at and count each character. When you hit a character whose ASCII code is 13, you know you're at the end of that line and can exit the loop for that line.

The Fortran intrinisic function IACHAR(C) returns the ASCII number of the character C. This is the approach I would take.
 
Last edited:
I don't think Mark44's code will work, because Fortran sequential file I/O is line-based not character-based. You never get to see the "end of line" characters.

The only "100% standard fortran" way to do this is open the file for direct access, with a record length of 1. Then you really can read every character in the file, and pick out the end-of-file marks.

download raw_io.f90 from https://orion.math.iastate.edu/burkardt/f_src/raw_io/raw_io.html and look at
RAW_OPEN and RAW_C_READ.

Many Fortran systems have easier (and more efficient) non-standard ways to do this, using subroutines that have the same I/O model as C. Look in your documentation for routines with names like getc, getchar, fgetc, etc.

Or, write your own I/O routines in C and call them from Fortran.

There may be a non-standard option in the OPEN statement that makes Mark44's idea work. Again, check your documentation.

Another non-standard method in earlier version of Fortran is the Q format descriptor.
Code:
integer len
character*1000 string
READ(*,"(Q,A)") len,string
sets len to the length of the input line, and reads the contents into string. You have to make string long enough to hold the longest line in the file.

If blank characters at the end of a line are not significant, you can read the whole line into a long character string. It will be padded with blanks at the end, so you can find where the last non-blank character is.

If all else fails, you can pre-process the file either with a C program or Unix utilities like awk, perl, etc, to append the original length of each line as a number - i.e. convert the file
ABC
DEFG
to something like
3 ABC
4 DEFG
 
Last edited by a moderator:
Yeah, it's been a while since I had a Fortran compiler, so I didn't test my ideas. If I were tasked with counting the number of characters in a file, writing code in Fortran would not be at the top of my list of approaches.
 
Thank you!
A lot of info, but I will try.
Its true what AlephZero says, the main problem was that I didn´t know how to go forward in a line, because fortran is line-based.
Maybe the raw input/output is too complex to make it now with my project, but it´s very interesting to do it in the future (XD), I was looking for it.
I didn´t know about the subroutines you mention, they are really helpfull (there are not any of them in my fortran 95 textbook, but I will search for it)
About making the character string, until now I had it like that, but it´s a little bit a mess.
Also very interesting the Unix utilities, maybe it is the easiest to implement. If I can´t do it with the subroutines I will try that.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
7K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K