Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Apr 5, 2014 #1
    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:

    read(unit=111,fmt="(1a)",iostat=s) line
    if (s<0) then

    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!!
  2. jcsd
  3. Apr 5, 2014 #2


    Staff: Mentor

    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.
    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: Apr 5, 2014
  4. Apr 5, 2014 #3


    User Avatar
    Science Advisor
    Homework Helper

    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 [Broken] and look at

    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 (Text):

    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
    to something like
    3 ABC
    4 DEFG
    Last edited by a moderator: May 6, 2017
  5. Apr 5, 2014 #4


    Staff: Mentor

    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.
  6. Apr 6, 2014 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook