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

  • Fortran
  • Thread starter hodei_cloud
  • Start date
  • Tags
    Fortran Line
In summary, the program needs to be able to count the number of characters in a line of text. The author tried a number of methods to do this, but none of them worked. He eventually found a way to do it using Fortran subroutines, but it was not easy. He also mentioned that if the file has blank characters at the end of the lines, it can be difficult to read the whole line. He also mentioned that if the file is too complex to read with his current project, he will try to implement raw input/output in the future.
  • #1
hodei_cloud
19
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
  • #2
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:
  • #3
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:
  • #4
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.
 
  • #5
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.
 

1. How do I read a line in Fortran 90?

In Fortran 90, you can use the READ statement to read a line of characters from a file. This statement will read the entire line, including any white spaces or special characters, and store it in a variable.

2. How can I count the number of characters in a line in Fortran 90?

You can use the LEN() function in Fortran 90 to count the number of characters in a line. This function takes a string as an argument and returns the length of the string, including any white spaces or special characters.

3. Can I read only a specific number of characters from a line in Fortran 90?

Yes, you can use the FORMAT specifier in the READ statement to specify the number of characters you want to read from a line. For example, if you only want to read the first 10 characters, you can use the format 'A10' in the READ statement.

4. Is there a way to ignore white spaces or special characters when reading a line in Fortran 90?

Yes, you can use the TRIM() function in Fortran 90 to remove any trailing white spaces from a line before counting the characters. This function takes a string as an argument and returns the string without any trailing white spaces.

5. How do I handle lines with varying lengths in Fortran 90?

You can use the INQUIRE statement in Fortran 90 to determine the length of a line before reading it. This statement will return the length of the line, allowing you to adjust the FORMAT specifier in the READ statement accordingly. Alternatively, you can use the TRIM() function to remove any trailing white spaces after reading the line.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
9
Views
6K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
2
Views
936
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
19
Views
6K
Back
Top