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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a Txt File of Unknown Length to a 1D Array 1

Status
Not open for further replies.

mwatson87

Technical User
May 28, 2013
5
GB
Hi Guys,

After the great and encouraging help that i received from my last post....

I have written a program that does a fourier transform on input Time Histories. As a novice (but keen learner) I'm very proud of my progress so far learning Fortran. The program works great so far but it is only applicable to TH's of the same length as I have specified the array size. The TH's can vary in length therefore I would like Fortran to be able to cope with this.

The step I need to take (i think) are:

1. Define a 1D Array of unknown size, f:))
2. Open the Text file containing the TH (single column containing real numbers)
3. Read the Text file
4. Allocate it to the 'undefined size' array, f

I therefore have produced the following code:

Code:
program Freq
	implicit none

	real, allocatable :: f(:)

	open (unit=1, file='C:\Users\Martin\Desktop\SRS\Freq.txt', status='old', action='read')
    
    	read(1, *) f
        allocate (f(:))
	
close(1)

print *, f

end program Freq

Note: This is just and extract from my overall program to illustrate the problem (hence why I have print *, so i can see if it worked). In my head (thinking like an engineer, not a programmer, probably where I am going wrong) this seems to make sense, as set out by the bullet points at the start. Hopefully this helps all the experts understand what I am thinking, and what my approach is.

I am a keen learner so any help would be appreciated. PS feel free to use layman's terms, as an engineer it often helps my understanding, learning Fortran for this program is not a quick fix but I have a general interest in properly learning the language :D

Thanks for your time,

Martin
 
You've got the horse before the cart. You need to allocate the space before reading the data in. There are several ways of doing this.

1) Don't bother about allocation: just declare a huge array that is way more that what you'll ever need. Read the data into that array until end of file. Use this if you have to stick to F77 which does not have dynamic allocation.
2) Read the data line by line into a dummy variable, counting the number of values, until end of file. Rewind the file, allocate an array for the correct number of values and then read them in again. Use this if you are not allowed to modify the data.
3) Modify the data: put in the count on the first line. Read in the count first, allocate and then read the rest of the data. This is quite easy in tools like notepad: just switch on the status bar, go to the last line and it will tell you how many values there are.
 
Firstly, thanks very much for your help and putting me on the right lines.

I am going with option 3) as it doesn't matter if i modify the file....as long as i don't include the first value in the array.

This seems to work but I dont know if its correct....or I got lucky ;) but it seems to work, do you have any advice or feedback :)

Code:
program Freq
	implicit none

		real, allocatable :: f(:)
		integer :: fmax
        
	open (unit=1, file='C:\Users\Martin\Desktop\SRS\Freq.txt', status='old', action='read')
      	
		read(1,*) fmax
		allocate (f(fmax))

        read(1,*) f
        
	close(1)

print *, f

end program Freq


Thanks again for the great help :)

Martin
 
Oh, I see...it's reading fmax and allocating f(fmax)...did not quite see the first time around and the indentation kept the other read statement out of sight
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top