Openning new files and write data into them

  • Thread starter Thread starter vjramana
  • Start date Start date
  • Tags Tags
    Data files
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to opening and writing data into multiple files in a loop. The context is primarily technical, focusing on coding practices in Fortran for file handling and data management.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Vijay describes a need to open four main data files and subsequently create four sub-files for each main file, totaling 16 sub-files, but encounters issues with file naming and overwriting.
  • Some participants inquire about the specifics of the error, asking whether it is a compilation issue or a runtime error.
  • Vijay clarifies that the program compiles but fails during execution due to attempts to open files with the same names for different main data files.
  • One participant suggests that the repeated opening of the same file names is problematic and proposes using a variable to dynamically generate unique file names based on the main data file unit number.
  • There is a suggestion to include a CLOSE() statement to properly close files before reopening them, which is necessary for using the same unit number again.

Areas of Agreement / Disagreement

Participants generally agree that the current approach leads to file name conflicts and that a solution involving dynamic file naming is necessary. However, there is no consensus on the best method to implement this solution, as some details remain unclear.

Contextual Notes

Participants have noted limitations regarding the handling of file names and the necessity of closing files before reopening them, but specific assumptions about the overall program structure and data flow are not fully explored.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K