forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading data from one file and storing the results in other?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Hamed



Joined: 15 Jan 2018
Posts: 2

PostPosted: Tue Jan 30, 2018 10:10 pm    Post subject: Reading data from one file and storing the results in other? Reply with quote

I want to read some raw data from one file say A and display the results after analysis/calculation in the other file say B.
I found the below code in fortran 95 tutorial is similar to one I am looking for but it shows some error, after execution.
In this code I need to know what is Unit .
Can any one run the below code and suggest any possible solution.
Code:
Program k
Implicit none
Real marks(100),sum
Integer n,i
sum=0
Open(unit=5,file="marks.dat")
Read(5,*) n
If(n<=0) Then
  close(5)
  Stop "Error:No data"
  Endif
  Read (5,*)(marks(i),i=1,n)
  Close(5)
  Print*, n,"values read as follows:"
  Print "(8f8.0)", (marks(i),i=1,n)
  Do i=1,n
    Sum=sum+marks(i)
    End do
    Print *, "(a,f6.2)", "The averagevalue is ", sum/n
    End Program K
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 697
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Tue Jan 30, 2018 11:46 pm    Post subject: Reply with quote

I have made a few changes to your code.

Code:
Program k
Implicit none
Real marks(100),sum
Integer n,i
sum=0
Open(unit=10,file="marks.dat",status='old')
Read(10,*) n
If(n<=0) Then
  close(10,status='keep')
  Stop "Error:No data"
Endif
Read (10,*)(marks(i),i=1,n)
Close(10,status='keep')
write(6,*) n,"   values read as follows:"
write(6,"(8f8.0)") (marks(i),i=1,n)
Do i=1,n
    Sum=sum+marks(i)
End do
write(6,"(a,f6.2)") "The averagevalue is ", sum/n
End Program K


Standard FORTRAN reserves two UNIT numbers for I/O to user. They are:

UNIT = 5 for INPUT from the keyboard with the READ statement

UNIT = 6 for OUTPUT to the screen with the WRITE statement

For file I/O I always use a unit number .gt. 10 and .lt. 99

This link should provide more guidance:

http://www.oc.nps.edu/~bird/oc3030_online/fortran/io/io.html
Back to top
View user's profile Send private message Visit poster's website
Kenneth_Smith



Joined: 18 May 2012
Posts: 697
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Wed Jan 31, 2018 10:58 pm    Post subject: Reply with quote

there is nothing obviously wrong in the code you messaged me with:-

Code:

Program k
Implicit none
Real marks(100),sum
Integer n,i
Character *50 file1
file1='marks.dat'
sum=0
Open(unit=10,file=file1,status='old')
Read(10,*) n
If(n<=0) Then
close(10,status='keep')
Stop "Error:No data"
Endif
Read (10,*)(marks(i),i=1,n)
Close(10,status='keep')
open(unit=20,file='output.dat',status='new')
write(20,*) n," values read as follows:"
write(20,"(8f8.0)") (marks(i),i=1,n)
Do i=1,n
Sum=sum+marks(i)
End do
write(6,"(a,f6.2)") "The averagevalue is ", sum/n
End Program K


my test input file contains:-
5
10,15,25,30,45

if the first line of your input file is blank i.e. contains no data you may get an error

if the first line is 5.0 i.e a real value you will get an error since you supply a real value when attempting to read an integer value.

You can get more information about the reason for the error by examining the variable assigned to IOSTAT, you can specify which line to jump to in the event of an error using ERR, and you can specify which line to jump to if you reach the end of the file whilst trying to read more data using END :-

IOSTAT=<variable>
<variable> will contain zero if the operation was successful, otherwise a positive value to indicate an error, -1 to indicate EOF or -2 to indicate EOR.

ERR=<label>
<label> specifies a label to go to in the event of an error.

END=<label>
<label> specifies a label to go to on end of file.

Example:

Code:
INTEGER :: IOstatus

READ(*,*,IOSTAT=IOstatus) var1, var2, ..., varn


The third component of the above READ is IOSTAT= followed by an INTEGER variable. The meaning of this form of READ is simple:
After executing the above READ statement, the Fortran compiler will put an integer value into the integer variable following IOSTAT=, IOstatus above. Based on the value of IOstatus, we have three different situations:

If the value of IOstatus is zero, the previous READ was executed flawlessly and all variables have received their input values. This is the normal case.

If the value of IOstatus is positive, the previous READ has encountered some problem. A commonly seen problem would be illegal data. For example, supplying a real number to an integer variable.

If IOstatus is positive, you cannot trust the values of the variables in the READ statement; they could all contain garbage values, or some of them are fine while the others are garbage.

If the value of IOstatus is negative, it means the end of the input has reached. Under this circumstance, some or all of the variables in the READ may not receive input values.
Back to top
View user's profile Send private message Visit poster's website
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Feb 02, 2018 5:39 am    Post subject: Reply with quote

I did an adaption to the code, so that "n" is not needed.
This works with FTN95, but not sure if standard conforming.
Read (10,*,iostat=iostat)(marks(i),i=1,n)
The approach is certainly useful when using read(lu,* for an unknown number of values, assuming the returned "I" occurred at the error.
Code:
 Program k
   Implicit none
   Real marks(100),sum
   Integer n,i, iostat
   Character *50 file1
   
   file1='marks.dat'
   Open (unit=10,file=file1,status='old')
   Read (10,*,iostat=iostat) n
   If (n<=0) Then
     close (unit=10)
     Stop "Error:No data"
   Endif
   write (*,*) n,' numbers expected'

   Read  (10,*,iostat=iostat)(marks(i),i=1,n)
   if ( iostat /= 0 ) then
     write (*,*) 'error reading mark no',i,' iostat=',iostat
     n = i-1
     if ( n <= 0) Stop "Error:No data"
     write (*,*) n,' numbers will now be processed'
   end if
   Close (unit=10,status='keep')
   
   open (unit=20,file='output.dat',status='unknown')
   write (20,*) n," values read as follows:"
   write (20,"(8f8.0)") (marks(i),i=1,n)
   write ( *,*) n," values read as follows:"
   write ( *,"(8f8.0)") (marks(i),i=1,n)
   
   sum=0
   Do i=1,n
     Sum=sum+marks(i)
   End do
   if ( n > 1) sum = sum/n
     
   write(20,"(a,f6.2)") "The average value is ", sum
   write( *,"(a,f6.2)") "The average value is ", sum
   Close (unit=20,status='keep')
 End Program K
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group