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

  • Thread starter Thread starter hodei_cloud
  • Start date Start date
  • Tags Tags
    Fortran Line
Click For Summary
The discussion centers around counting characters in each line of a text file using Fortran 90. The initial code successfully counts the number of lines but does not address character counting within those lines. Participants suggest various methods to achieve this, highlighting that Fortran's sequential file I/O is line-based, making direct character access challenging. Recommendations include using a loop to examine each character until reaching the end of the line, utilizing the intrinsic function IACHAR to get ASCII values, and considering non-standard options like direct access file opening or using the Q format descriptor to read lines with their lengths. The idea of preprocessing the file with external tools like C programs or Unix utilities is also mentioned as a practical alternative. Overall, the conversation emphasizes exploring both standard and non-standard approaches to effectively count characters in Fortran.
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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 9 ·
Replies
9
Views
6K
Replies
8
Views
2K
  • · 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