Fortran help: Open and Write functions

In summary, the individual is asking how to call a file name in a write statement and how to suppress or change the formatting of certain characters in their code. The expert suggests using a variable of type CHARACTER for the file name and replacing the ? in the FORMAT statement with an A descriptor. They also explain that the formatting of characters or words in the code is not part of the Fortran syntax and is simply a convenience offered by some text editors.
  • #1
Mustachio13
2
0
How can I have an open statement:

OPEN(Unit=1,File='data.txt',...)

and be able to later have a write statement:

WRITE(*,15)
15 FORMAT('Data was collected from ',?,'file:')

where the ? would call in the file name. Also, when I type 'file:', it creates a link to something. How do I stop that or do I need to just do something else instead?
 
Technology news on Phys.org
  • #2
'data.txt' can be the value of a variable of type CHARACTER. You would replace the ? in yourFORMAT statement with an A descriptor and use the name of the CHARACTER variable on the corresponding WRITE statement.

For your last question, could you clarify what you are asking?
 
  • #3
Thank you, I should have realized that sooner. With my second question, when I went to type my write statement, once I had the ':' after file, it became blue and underlined. I am still new to fortran and I have never seen or had to use a feature that did that. Is it something that can be suppressed or do I have to use a different character instead of ':' after the word 'file'?
 
  • #4
Mustachio13 said:
With my second question, when I went to type my write statement, once I had the ':' after file, it became blue and underlined.

What are you using for writing your code? That sounds like something Microsoft Word would do.
 
  • #5
Some programming editors use different colors to display statements according to their use in the program. For instance, declaration statements may be one color, comments another, executable statements another, etc. The final color sometimes cannot be determined until the statement is completely entered.
 
  • #6
Needless to say, this highlighting of characters or entire words has nothing to with the Fortran program per se and are not part of the syntax, nor persist once you save the file to disk and quit whatever editor you are using...it is simply a convenience offered by some text editors to make it easier to the human eye to tell things apart...just wanted to mention this since nobody seemed to have said it and it seems that you could benefit from such commentary, too.
 

Related to Fortran help: Open and Write functions

Question 1: What is the purpose of the Open function in Fortran?

The Open function in Fortran is used to open a file for reading, writing, or appending data. It allows the user to specify the file name, access mode, and other parameters that control how the file is opened.

Question 2: How do I use the Open function in Fortran?

To use the Open function, you need to first declare a file variable and specify its properties, such as the file name, access mode, and file status. Then, you can call the Open function with the file variable as its argument. Make sure to check for any errors returned by the function before proceeding with reading or writing data to the file.

Question 3: What is the difference between the Write and Print functions in Fortran?

The Write function in Fortran is used to write data to a file, while the Print function is used to display data on the screen. The Write function allows for more control over the formatting of the data being written, while the Print function automatically formats the data for display.

Question 4: Can I open and write to multiple files using the Open and Write functions in Fortran?

Yes, you can open and write to multiple files using the Open and Write functions in Fortran. You just need to declare separate file variables for each file and call the functions with the appropriate file variable as the argument.

Question 5: How do I close a file in Fortran after using the Open and Write functions?

To close a file in Fortran, you can use the Close function with the file variable as its argument. It is important to close the file after you are finished writing data to it to ensure that all changes are saved and that the file is available for other processes to access.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
924
  • Programming and Computer Science
Replies
4
Views
693
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
8
Views
715
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top