tags:

views:

2186

answers:

2

Hello,

I am trying to read a formatted 2D array from a file on disk into a variable. I have the write operation, which is rather simple, but am stuck on reading the same file. Could someone point me to a sample/writeup on how to do this? The net seems saturated, but i can't find a useful article.

Btw, the reason for the formatted file is to keep it human readable as it contains configuration options.

Thank you in advance.

+2  A: 

I've actually found that the physical documentation that comes with the compiler is generally the most readable and most informative for Fortran compilers. Of course, that's not an option if you're using g95 or something like that.

Here's a pretty good page describing most of the technical specs of the read statement. Particularly, see the section on "Format Edit Descriptors" - very handy.

On a side note, if you have the exact write format string, you can usually drop that into a read format string, but if you're writing with WRITE(*,*) or something like that, you probably won't have a valid write format statement to use.

Finally, if you're dumping this out to ASCII so people can read it, and you don't have to worry about backward compatibility, consider dumping everything out as fixed-length fields, as they are by far the easiest things to read back in.

Sorry I can't think of better online resources, but Fortran is woefully underdocumented on the web. I remember once checking to see if g95 had Fortran reference docs, but they mostly only have docs on their specific compiler settings. Good luck, though!

Mike
Thank you Mike, I had actually just read through that page. I think i am close. I am just running into an end-of-record error. Ahh, the fun of a new language :)
ccook
sorry, end-of-file is the error
ccook
If you're specifying a set record length, you probably don't need to - reading and writing ASCII usually isn't done with record sizes, but there are always exceptions :) If you're hitting EOF before you think you should, that's a different story.
Mike
Well, when reading an array, should i not specify the length? I two values specifying the size of a 2D array preceeding the array values. I am using:READ(iFile,*) (nb2(i),i=1,2)to read 'two' values. Thank you again Mike
ccook
Okay, this got strange. I removed the file from disk and started over. Seems to be working now! :D
ccook
Wow! just read your last two comments. Strange. Do you know if you might have had an extra crlf in there, or maybe no crlf when your code was expecting one?
Mike
I think the file was being cleared by some code above... (The part i used to write a sample file) Kind of noobish really. So the end-of-file was just that, the end of the file. Beautiful hahah.
ccook
As an aside, I am really liking this method for reading/writing. (I am a c# dev of 5yrs)
ccook
I know what you mean - it is VERY clean, and does exactly what you tell it to do, every time!
Mike
+1  A: 

Could the OP post the code finally used? I'm struggling with the same problem and cannot find an answer anywhere.

In particular I wonder if one can read line by line using something like

do line=1,nlines
   read(fid,*) myarray(line,:)
enddo

or if one has to loop over all the numbers on each line too, using non-advancing reads.

Any help is appreciated.

TD

PS: Sorry for posting a new "answer". I'm new here and couldn't find out how to add to the comments to the answer by Mike.

ThulDai
I looked, but, couldnt find this particular code. I dont think you will have to read by line or by number. If the format is right, it should be as simple as READ(fid,*) myarray. That will fill the array. Was Mike's link helpful?
ccook