Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a file into an array of values 2

Status
Not open for further replies.

Widukin

Programmer
Feb 8, 2003
11
FR
I suppose that my question is rather symilar to the "reading until EOF" thread... However, my problem is as follows:

(On the read command, the help index on my fortran90 isn't being very helpfull....)

I have a file with the 3d mesh from another program.
The list is as follows:


1 530 493 524 453
2 524 530 453 579
3 530 493 453 548
4 377 346 371 277
5 377 346 277 351
6 28 11 23 20
7 28 11 20 15
(...)


I want open the file (using the read statement)
Then I want to transform every element of that file into an item of an array like the one that follows:

(/ (/1,530,493,524,453/), (/2,524,530,453,579/), ... /)

So that I can use this for the rest of my computations.


I thank you in advance for any help you can provide,

Widukin
 
Assuming your data is fixed format, and the numbers are right justified, try this. You will have to alter the format statement to accurately reflect the layout of the data line from the file (can't tell from your post).

INTEGER I
INTEGER ARR_VAR(500,5)

OPEN(40,FILE='MYFILE.DAT')

I = 0
10 CONTINUE
I = I + 1
IF (I.GT.500) GOTO 20
READ(40,FMT='(I3, I5, I5, I5, I5)',END=20)
& ARR_VAR(I,1), ARR_VAR(I,2), ARR_VAR(I,3),
& ARR_VAR(I,4), ARR_VAR(I,5)
GOTO 10
20 CONTINUE
IF (I.GT.500) WRITE(6,*) ' *** OVERFLOW ***'

CLOSE(40)
 
Thank you very much!! :)

I'll first try what you said and then give you some feedback.

Widukin
 
I've been working on a program based on the code you provided me with. It reads and I can record the values into an array, however, the blank spaces are substituded by a zero. Isn't there a way for the fortran read to ignore blank spaces. (I remember such a thing in C, had something to do with the ascii code of the blank space...). Isn't there a way for fortran to do the same?
The real problem is that my mesh file has about 3000 lines of the type I showed on this thread's first post. And the amount of zeros that would be absorbed into the read array is astronomical.

Could you help me on this? :)

Widukin
 
If your input file is fixed format, use the "X" character in your format string to identify (and ignore) where the spaces are in your file. For instance, if each line in your file is defined as follows:

1 blank
3 digit number
6 blanks
5 digit number
3 blanks
5 digit number
3 blanks
5 digit number
3 blanks
5 digit number

Then use the following format statement:

FMT='(1X,I3,6X,I5,3X,I5,3X,I5,3X,I5)'

Be sure to make your "I" formats large enough to fit the largest possible number in your input file. A blank to the left of a number will be interpretted as zero. In other words, if your largest number will be 99999, then use I5. Using I5, each of the following 5 characters would result in the same number:

123
00123
0 123
0123

FYI, you can also use "X" to ignore other characters in your file as well. Doesn't have to be a blank. Hope this helps.

Jeff
 
It's done and working!
:)

Thank you both very much!

Widukin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top