Openning new files and write data into them

In summary: Firstly, you're opening the same file over and over. Secondly, since you're not CLOSE()ing those files, you can't reopen the same file using the same unit number.
  • #1
vjramana
15
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5


Hello Vijay,

Thank you for sharing your code. It looks like you are trying to open multiple files and split data from one file into multiple sub-files. However, it seems like there may be some issues with your code as it is not working.

First, make sure that you are using the correct file names and file paths for each file you are trying to open. Also, double check that the file paths are correct and that the files exist in the specified location.

Next, it may be helpful to include some error handling in your code, so that if there is an issue with opening or writing to a file, the program will stop and display an error message. This can help you troubleshoot any issues with your code.

Additionally, make sure that you are using the correct file units for each file. You have used the units 100-103 for your main-data-files, but have used units (10+kk) for your sub-files. It may be better to use a different set of units for your sub-files, such as (200+kk), just to avoid any potential conflicts.

Lastly, make sure that your code is properly formatted and that the logic is correct. For example, in your do loop, you are trying to read data from the main-data-file, but you are not specifying which file unit to read from. You may need to use the "kk" variable in your read statement to specify which main-data-file you are reading from.

I hope this helps. Good luck with your code!
 

Related to Openning new files and write data into them

1. How do I open a new file for writing data?

To open a new file for writing data, you can use the "open()" function in your programming language of choice. This function takes in the name of the file and the mode in which you want to open it (in this case, "w" for write mode). For example, in Python, you would use the code "file = open("myfile.txt", "w")" to open a new file named "myfile.txt" for writing.

2. What types of data can I write into a new file?

You can write any type of data into a new file, as long as you convert it to a string first. This includes numbers, strings, lists, and dictionaries. In most programming languages, you can use the "str()" function to convert data to a string before writing it to a file.

3. How do I write data into a specific location in the file?

In most programming languages, you can use the "seek()" function to move the cursor to a specific location in the file before writing data. This function takes in the offset from the beginning of the file and the position where the cursor will be moved (0 for the beginning, 1 for the current position, and 2 for the end).

4. Can I write data to a file that already has existing data?

Yes, you can write data to a file that already has existing data. However, you will need to use the "a" mode instead of the "w" mode when opening the file. This will append the new data to the end of the file instead of overwriting the existing data.

5. How do I ensure that the data I write to the file is saved?

In most programming languages, you can use the "close()" function to ensure that the data written to the file is saved. This function closes the file and saves any changes made to it. It is important to always remember to close files after writing data to them to avoid data loss.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top