Openning new files and write data into them

  • Thread starter Thread starter vjramana
  • Start date Start date
  • Tags Tags
    Data files
AI Thread Summary
The discussion revolves around a coding issue where a loop is intended to open four main data files and subsequently create four sub-files for each main file, resulting in a total of 16 sub-files. The user, Vijay, encounters an error during execution because the code attempts to open sub-files with the same names for each main file, leading to conflicts. The solution proposed involves dynamically generating unique file names for each set of sub-files by appending the unit number to the file names. This can be achieved by using a formatted write statement to create distinct names for each iteration of the loop. Additionally, it is highlighted that the files should be closed after use to avoid issues with reopening them. The conversation emphasizes the importance of managing file names and ensuring proper file handling within the loop structure.
vjramana
Messages
13
Reaction score
0
Hi,

I am trying to write a loop to open four main-data-files. At each open of main-data-file, I want the loop to open another four sub-files and split the data from mail-data-file according to the creteria and dump into the four files. And the process goes on. That means I will have total of 16 sub files for the four main-data-files.

I have tried to write the code as below but it doesn't seems to work.

***************************************************************************************************
program final
implicit none
!
integer :: i,j, k, m, jj, kk
integer, parameter :: dist_lines=256
real, DIMENSION(dist_lines) :: dist

! main-data-files
open(unit=100,status="old",file="selected_MAXdistance_only_malto_THERMO_1st.dat")
open(unit=101,status="old",file="selected_MAXdistance_only_malto_THERMO_2nd.dat")
open(unit=102,status="old",file="selected_MAXdistance_only_malto_THERMO_3rd.dat")
open(unit=103,status="old",file="selected_MAXdistance_only_malto_THERMO_4th.dat")

do kk = 100,103

! sub-files
open(unit=(10+kk), file="maximum_dist_001_064.dat")
open(unit=(11+kk), file="maximum_dist_065_128.dat")
open(unit=(12+kk), file="maximum_dist_129_192.dat")
open(unit=(13+kk), file="maximum_dist_193_256.dat")

read(kk,'(F8.3)') (dist(i), i = 1,dist_lines)

do k = 1, 256

if (k < 65 ) then
write ((kk+10),'(F8.3)'), dist(k)
elseif ( k >=65 .and. k < 129 ) then
write( (kk+11),'(F8.3)'), dist(k)
elseif ( k >=129 .and. k < 193 ) then
write ((kk+12),'(F8.3)'), dist(k)
elseif ( k >=193 .and. k < 257 ) then
write ((kk+13),'(F8.3)'), dist(k)
end if


end do

end do

stop
end

Could anyone help ?
Thanks in advance.
Vijay
 
Technology news on Phys.org
When you say it doesn't work, can you be more specific?

Does it fail to compile?

If it compiles, does the program give incorrect results?
 
Last edited:
It compiles. There is no problem. But when I execute it stops says error. The error indicates that the files (maximum_dist_001_064.dat) open ready.

That means in the first call of the main-data-file ( UNIT=100) it opens four files. Thay are

maximum_dist_001_064.dat
maximum_dist_065_128.dat
maximum_dist_129_192.dat
maximum_dist_193_256.dat

When it calles the second file (UNIT=101), it tries to open another four files in the same name. So it stopes. I don't know how to make the four files to generate new names for each main-data-calling.

For example,

When it called file UNIT=100, the new four files might look like,

maximum_dist_001_064_100.dat
maximum_dist_065_128_100.dat
maximum_dist_129_192_100.dat
maximum_dist_193_256_100.dat

and when it called the second main-data-file, it might name as below

maximum_dist_001_064_101.dat
maximum_dist_065_128_101.dat
maximum_dist_129_192_101.dat
maximum_dist_193_256_101.dat

This allow me to know that the file generated are from calling UNIT=100, UNIT=101 and so on..

Hope this explain correctly how this code suppose to work.
Thanks in advance.
 
Right now you're opening the same files over and over. Plus, since you're not CLOSE() those files, you can't reopen the same file using the same unit number.
Code:
do kk = 100,103

! sub-files
open(unit=(10+kk), file="maximum_dist_001_064.dat")
That line will open up the file maximum_dist_001_064.dat everytime through the loop. If you want to open up a different filename each time through the loop, you can WRITE to a variable such as:
Code:
DO kk=100,103
WRITE(fname1,'(A21,I3.3,A4)') 'maximum_dist_001_064_',kk,'.dat'
OPEN(21,file=fname,form='formatted')
. . . .
END DO
I'm not sure exactly what you're trying to do here, but it seems a little messed up in a few ways.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
6
Views
2K
Replies
3
Views
3K
Replies
11
Views
3K
Replies
1
Views
1K
Replies
8
Views
2K
Back
Top