Home > DeveloperSection > Forums > How to Convert Integers to String in Fortran?
Pawan Shukla
Pawan Shukla

Total Post:29

Posted on    November-19-2014 2:11 AM

 ASP.Net C# 

 1 Reply(s)
 1000  View(s)
Rate this:

I have a program in Fortran that saves the results to a file. At the moment I open the file using

OPEN (1, FILE = 'Output.TXT')

However, I now want to run a loop, and save the results of each iteration to the files 'Output1.TXT', 'Output2.TXT', 'Output3.txt', ...

Is there an easy way in Fortran to constuct filenames from the loop counter i?

Kamlakar Singh
Kamlakar Singh

Total Post:28

Posted on    November-19-2014 6:59 AM

you can write to a unit, but you can also write to a string

program foo

    character(len=1024) :: filename

    write (filename, "(A5,I2)") "hello", 10

    print *, trim(filename)

end program

Please note (this is the second trick I was talking about) that you can also build a format string programmatically.

program foo

    character(len=1024) :: filename

    character(len=1024) :: format_string

    integer :: i

    do i=1, 10

        if (i < 10) then

            format_string = "(A5,I1)"


            format_string = "(A5,I2)"


        write (filename,format_string) "hello", i

        print *, trim(filename)


end program

Don't want to miss updates? Please click the below button!

Follow MindStick