Retake: Reading an existing file into a fortran code

In summary, the conversation discusses a program that calculates monthly rainfall by reading values from a file. The program successfully executes when the file is placed in the same directory and opened before executing. However, there is an issue with negative values in the data file, and the user is seeking a way to read only positive inputs. The solution suggested is to test each value read and skip any negative values. The code is also provided in the conversation.
  • #1
angelfaz
2
0
Thank you very much for the reply. I placed the file in the same directory as the program and opened the file before executing and everything was just perfect. But my other problem is that, in my data, there consists of negative numbers. Is there a way to read the positive inputs without the negative ones? I tried adding an IF statement to the code, but still the negative is being read.
I am attaching the a sample of the code i wrote.
Thanks very much
 

Attachments

  • OPENFILE.docx
    11.7 KB · Views: 234
Technology news on Phys.org
  • #2
It's not clear why you don't want to read negative values from your data file.

Can you just test the value read to see if it is less than zero and discard it?
 
  • #3
OP, here is your code. I don't see the point in attaching a Word document instead of inserting the code directly into the input pane here.
angelfaz said:
I tried adding an IF statement to the code, but still the negative is being read.
Where? I don't see that IF statement in your code. It's pretty simple really - each time you read from the file check to see if the number is negative. If so, skip to the next loop iteration.

Code:
Program rainfall
!Purpose:
!This program calculates the total monthly rainfall and the average by reading in the values from an opened file.
 IMPLICIT NONE
!List of variables:
CHARACTER(len = 20):: filename  !Input file name
CHARACTER(len = 20)::month      !Month to be read
INTEGER::n = 0  !Number of input values.
INTEGER:: ierror  !Status flag of i/o statement
REAL:: x  !An input data value
REAL:: sum_x = 0  ! Sum of input values
REAL:: x_bar = 0  !Average of input samples

!Prompt the user and get the name of file for input
WRITE(*,*)'enter the name of the file:'
READ(*,*) filename

!Prompt the user for the month
WRITE(*,*)' enter the month:'
READ(*,*) month
!Open the input file 
OPEN (Unit = 3, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT = ierror)
 !Check to see if the file opened successfully
errorcheck: IF (ierror > 0) THEN
 WRITE(*,*) filename
ELSE
!file opened successfully. Read input values x from the file
DO
   READ ( 3,*,iostat= ierror)  x
IF ( ierror /= 0) EXIT
 n = n + 1
sum_x = sum_x + x         !Calculate sum

END DO

!Calculate the mean rainfall
x_bar = sum_x / real(n)

!Tell user

WRITE(*,*)'Number of data points = ',n
WRITE(*,*)'Monthly total = ', sum_x
WRITE(*,*)'Monthly average = ', x_bar

!Close input file
CLOSE(Unit = 8)

End if errorcheck

End program rainfall
 

1. How do I read an existing file into a Fortran code?

To read an existing file into a Fortran code, you can use the "open" statement followed by the "read" statement. The open statement specifies the file to be read, and the read statement reads the data from the file into your program.

2. What format should the existing file be in for it to be read into a Fortran code?

The existing file should be in a plain text format, such as a .txt or .dat file, for it to be read into a Fortran code. It should also be formatted according to the specifications of your Fortran program, such as the number of columns and data types.

3. Can I read multiple files into a Fortran code at once?

Yes, you can read multiple files into a Fortran code by using multiple open and read statements, each specifying a different file to be read. You can also use a loop to read multiple files with similar formats into your program.

4. How do I handle errors while reading an existing file into a Fortran code?

You can use the "iostat" parameter in the read statement to check for errors while reading the file. If an error occurs, the value of "iostat" will be non-zero, and you can use an "if" statement to handle the error accordingly.

5. Can I modify the existing file while it is being read into the Fortran code?

Yes, you can modify the existing file while it is being read into the Fortran code. However, keep in mind that any changes made to the file will also affect the data being read into your program. It is recommended to make a copy of the file before modifying it to avoid any unexpected errors.

Similar threads

  • Programming and Computer Science
Replies
2
Views
919
  • Programming and Computer Science
Replies
20
Views
532
  • Programming and Computer Science
Replies
1
Views
554
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
3
Views
397
  • Programming and Computer Science
Replies
4
Views
622
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top