Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:48+00:00 2026-05-23T13:07:48+00:00

I am new to Fortran, and I would like to be able to write

  • 0

I am new to Fortran, and I would like to be able to write a two-dimensional array to a text file, in a row-wise manner (spaces between columns, and each row on its own line). I have tried the following, and it seems to work in the following simple example:

PROGRAM test3
  IMPLICIT NONE

  INTEGER :: i, j, k, numrows, numcols
  INTEGER, DIMENSION(:,:), ALLOCATABLE :: a

  numrows=5001
  numcols=762
  ALLOCATE(a(numrows,numcols))
  k=1
  DO i=1,SIZE(a,1)
    DO j=1,SIZE(a,2)
      a(i,j)=k
      k=k+1
    END DO
  END DO

  OPEN(UNIT=12, FILE="aoutput.txt", ACTION="write", STATUS="replace")
  DO i=1,numrows
    WRITE(12,*) (a(i,j), j=1,numcols)
  END DO
END PROGRAM test3

As I said, this seems to work fine in this simple example: the resulting text file, aoutput.txt, contains the numbers 1-762 on line 1, numbers 763-1524 on line 2, and so on.

But, when I use the above ideas (i.e., the last fifth-to-last, fourth-to-last, third-to-last, and second-to-last lines of code above) in a more complicated program, I run into trouble; each row is delimited (by a new line) only intermittently, it seems. (I have not posted, and probably will not post, here my entire complicated program/script–because it is rather long.) The lack of consistent row delimiters in my complicated program/script probably suggests another bug in my code, not with the four-line write-to-file routine above, since the above simple example appears to work okay. Still, I am wondering, can you please help me think if there is a better row-wise write-to-text file routine that I should be using?

Thank you very much for your time. I really appreciate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:07:48+00:00Added an answer on May 23, 2026 at 1:07 pm

    There’s a few issues here.

    The fundamental one is that you shouldn’t use text as a data format for sizable chunks of data. It’s big and it’s slow. Text output is good for something you’re going to read yourself; you aren’t going to sit down with a printout of 3.81 million integers and flip through them. As the code below demonstrates, the correct text output is about 10x slower, and 50% bigger, than the binary output. If you move to floating point values, there are precision loss issues with using ascii strings as a data interchange format. etc.

    If your aim is to interchange data with matlab, it’s fairly easy to write the data into a format matlab can read; you can use the matOpen/matPutVariable API from matlab, or just write it out as an HDF5 array that matlab can read. Or you can just write out the array in raw Fortran binary as below and have matlab read it.

    If you must use ascii to write out huge arrays (which, as mentioned, is a bad and slow idea) then you’re running into problems with default record lengths in list-drected IO. Best is to generate at runtime a format string which correctly describes your output, and safest on top of this for such large (~5000 character wide!) lines is to set the record length explicitly to something larger than what you’ll be printing out so that the fortran IO library doesn’t helpfully break up the lines for you.

    In the code below,

      WRITE(rowfmt,'(A,I4,A)') '(',numcols,'(1X,I6))'
    

    generates the string rowfmt which in this case would be (762(1X,I6)) which is the format you’ll use for printing out, and the RECL option to OPEN sets the record length to be something bigger than 7*numcols + 1.

    PROGRAM test3
      IMPLICIT NONE
    
      INTEGER :: i, j, k, numrows, numcols
      INTEGER, DIMENSION(:,:), ALLOCATABLE :: a
      CHARACTER(LEN=30) :: rowfmt
      INTEGER :: txtclock, binclock
      REAL    :: txttime, bintime
    
      numrows=5001
      numcols=762
      ALLOCATE(a(numrows,numcols))
      k=1
      DO i=1,SIZE(a,1)
        DO j=1,SIZE(a,2)
          a(i,j)=k
          k=k+1
        END DO
      END DO
    
      CALL tick(txtclock)
      WRITE(rowfmt,'(A,I4,A)') '(',numcols,'(1X,I6))'
      OPEN(UNIT=12, FILE="aoutput.txt", ACTION="write", STATUS="replace", &
           RECL=(7*numcols+10))
      DO i=1,numrows
        WRITE(12,FMT=rowfmt) (a(i,j), j=1,numcols)
      END DO
      CLOSE(UNIT=12)
      txttime = tock(txtclock)
    
      CALL tick(binclock)
      OPEN(UNIT=13, FILE="boutput.dat", ACTION="write", STATUS="replace", &
           FORM="unformatted")
      WRITE(13) a
      CLOSE(UNIT=13)
      bintime = tock(binclock)
    
      PRINT *, 'ASCII  time = ', txttime
      PRINT *, 'Binary time = ', bintime
    
    CONTAINS
    
        SUBROUTINE tick(t)
            INTEGER, INTENT(OUT) :: t
    
            CALL system_clock(t)
        END SUBROUTINE tick
    
        ! returns time in seconds from now to time described by t
        REAL FUNCTION tock(t)
            INTEGER, INTENT(IN) :: t
            INTEGER :: now, clock_rate
    
            call system_clock(now,clock_rate)
    
            tock = real(now - t)/real(clock_rate)
        END FUNCTION tock
    END PROGRAM test3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to edit the Fortran code that is referred
I'm very new to Fortran. Currently I'm writing (or trying to write) a fortran
I am trying to write a batch file to iteratively execute a fortran compiled
I need to write some data to file in Fortran 90. How should I
New to Node.js and Express, I am trying to understand the two seems overlapping
I am very new to Python, I need to read numbers from a file
I'm new to Fortran and just doing some simple things for work. And as
Fortran->Algol->Cpl->Bcpl->C->C++->Java ..... Seems like every language is built upon an ancestor language. My question
I'm new to fortran and i'm trying to execute this as an output. program
New to Fortran (just started today), having trouble with the natural logarithm: PROGRAM log

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.