Retake: Reading an existing file into a fortran code

Click For Summary
SUMMARY

The discussion centers on reading data from a file in Fortran while filtering out negative values. The original code provided by the user does not effectively discard negative numbers despite attempts to implement an IF statement. The solution proposed involves checking each read value and skipping the iteration if the value is negative. The program calculates total and average rainfall based on the positive values read from the input file.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with file I/O operations in Fortran
  • Knowledge of control structures, specifically IF statements
  • Basic concepts of data types and variable declaration in Fortran
NEXT STEPS
  • Implement error handling for file I/O in Fortran
  • Learn about Fortran DO loops and their control mechanisms
  • Explore advanced data filtering techniques in Fortran
  • Study the use of arrays for storing and processing multiple data values in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, data analysts working with numerical data, and students learning file handling and data processing in programming.

angelfaz
Messages
2
Reaction score
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

Technology news on Phys.org
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?
 
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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
1
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K