Question

Let's say that I would like output a file that should have some fixed header lines (i.e., independent from the calculations realized by my program), followed by information generated by my code, such as:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Morbi ut est in augue rhoncus lobortis.
0.10, 0.31, 0.73
0.34, 0.64, 0.27

Where the first two lines are always the same, but the last two are given by the code (for the sake of this example, consider them as 6 random floats). One way of producing this is to use the following code:

[...]
open(unit=11,file="output.TXT")
write(11,*) "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
write(11,*) "Morbi ut est in augue rhoncus lobortis."
write(11,*) x(1,1:3)
write(11,*) x(2,1:3)
[...]

Since the mentioned header has only two lines, this method works very well. Now let's say that the header would be 1000 lines long. This method obviously will not be the best way to solve this problem. On the other hand, it would be possible to create a file named output.TXT, write the header on it, and then open it via the following statement:

[...]
open(unit=11,file="output.TXT",position="APPEND")
[...]

Which would make sure that the Fortran output will be written after the header.

Now, I would like to find a method (if such one exists) that could handle such large headers WITHOUT having to give the user the file output.TXT. In other words: I would like to be able to store the header information on my code and then output it on the mentioned file, but to do it by some other method than the laborious write(11,*) "text line 1", write(11,*) "text line 2", etc.

Does such a thing exist in Fortran? Can anyone point me in the right direction?

Was it helpful?

Solution

You could declare character (len=80), dimension (1000) :: lines and then have 1000 statements line(1) = "xyz", etc. Then have a loop to write the lines. The write part is easy. The is no way to imbed raw text into Fortran source ... you have to place it into a variable with a DATA or assignment statement.

To me, it would be easier to have a standard file that the code reads, then copies to the output file. You could have an environment variable to designate the file or its directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top