Fortran-write file to directory other than current

In summary, the conversation discusses how to manage outputs from a fortran f90 program by writing them to a specific folder, thus keeping the program directory clean. The solution involves declaring a path and adding it to the file name.
  • #1
ign
5
0
Hello,

I am running a fortran f90 program and am writing outputs to several files,
the thing is I would like them to be written to a specific folder to better manage outputs
and so leaving the program directory clean.

here's a sample write sentence:

open(unit=3, file=val(i,1)//'_Brutes_lowV_thr_5_10_15.txt', status='replace')
do h=1, size(pmv)
write(3,*) YearList(h), pmv(h), lowV(h,:)
enddo
close(3)

where do I add instructions so that the file is written say in
an output folder located on the desktop (/home/ign/desktop/output)?

thank you in advance,
ign
 
Technology news on Phys.org
  • #2
solved the problem!

and to make it cleaner I declared a path

CHARACTER*29 :: path='/home/.../output/'

I then added path (followed by union slashes //) at the beginning of the file name

open(unit=3, file=path//val(i,1)//'_Brutes_lowD.txt', status='unknown')
 
  • #3
Thanks. this solved my problem too :)
 

1. How can I write a file to a directory other than the current one in Fortran?

To write a file to a specific directory in Fortran, you can use the OPEN statement and specify the FILE argument as the full path to the desired directory, followed by the desired file name. For example, OPEN(UNIT=1, FILE='/home/user/Documents/output.txt') will write the file output.txt to the /home/user/Documents directory.

2. Can I specify a relative path instead of the full path when writing a file in Fortran?

Yes, you can specify a relative path when writing a file in Fortran. For example, if your current directory is /home/user/Documents and you want to write the file output.txt to the /home/user/Documents/new_folder directory, you can use the OPEN statement with the FILE argument as OPEN(UNIT=1, FILE='new_folder/output.txt').

3. What happens if I try to write a file to a directory that does not exist?

If you try to write a file to a directory that does not exist, Fortran will give an error message and the file will not be created. Make sure the directory exists before attempting to write a file to it.

4. Is it possible to create multiple subdirectories when writing a file in Fortran?

Yes, it is possible to create multiple subdirectories when writing a file in Fortran. You can use the OPEN statement with the FILE argument as OPEN(UNIT=1, FILE='new_folder/subfolder/output.txt') to create a file named output.txt inside the subfolder directory, which is inside the new_folder directory.

5. Can I specify the file name and extension when writing a file in Fortran?

Yes, you can specify the file name and extension when writing a file in Fortran. Simply include the desired file name and extension in the FILE argument of the OPEN statement. For example, OPEN(UNIT=1, FILE='output.txt') will create a file named output.txt in the current directory.

Similar threads

  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
9
Views
867
  • Programming and Computer Science
Replies
2
Views
890
Replies
19
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top