FAQs on Fortran Programming Tutorials (Revised) : 025 : Reading and Writing to files - Simple Video Lecture - Introduction to Fortran Programming (Basic Level) - Database Management
1. How can I read data from a file in Fortran? |
|
Ans. To read data from a file in Fortran, you can use the READ statement. This statement allows you to specify the file unit number, the format of the data, and the variables where the data will be stored. Here's an example:
```fortran
READ(10, *) variable1, variable2, ...
```
In this example, 10 is the file unit number, and the asterisk (*) indicates that the format of the data is free format. The variables variable1, variable2, ... are the variables where the data will be stored.
2. How can I write data to a file in Fortran? |
|
Ans. To write data to a file in Fortran, you can use the WRITE statement. This statement allows you to specify the file unit number, the format of the data, and the values you want to write. Here's an example:
```fortran
WRITE(10, *) value1, value2, ...
```
In this example, 10 is the file unit number, and the asterisk (*) indicates that the format of the data is free format. The values value1, value2, ... are the values you want to write to the file.
3. How can I open a file for reading in Fortran? |
|
Ans. To open a file for reading in Fortran, you can use the OPEN statement. This statement allows you to specify the file unit number, the file name, and the file access mode. Here's an example:
```fortran
OPEN(10, FILE='filename', STATUS='OLD')
```
In this example, 10 is the file unit number, 'filename' is the name of the file you want to open, and 'OLD' is the file access mode indicating that you want to open the file for reading.
4. How can I open a file for writing in Fortran? |
|
Ans. To open a file for writing in Fortran, you can use the OPEN statement. This statement allows you to specify the file unit number, the file name, and the file access mode. Here's an example:
```fortran
OPEN(10, FILE='filename', STATUS='NEW')
```
In this example, 10 is the file unit number, 'filename' is the name of the file you want to create or overwrite, and 'NEW' is the file access mode indicating that you want to open the file for writing.
5. How can I close a file in Fortran? |
|
Ans. To close a file in Fortran, you can use the CLOSE statement. This statement allows you to specify the file unit number of the file you want to close. Here's an example:
```fortran
CLOSE(10)
```
In this example, 10 is the file unit number of the file you want to close. Once you close the file, you won't be able to read from or write to it anymore.