Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Error on Read File

Status
Not open for further replies.

sauber

Technical User
Mar 19, 2012
4
0
0
US
I am in mechanical engineering school and have to take Fortran. I can not figure out how to fix the error. There is an error in line 34. The program is in at the url below. Im sure i am just missing something easy.

Input:
1.1, 1.01
2.2, 2.30
3.3, 3.05
4.4, 4.28
5.5, 5.75
6.6, 6.48
7.7, 7.84

Thank You
 
Sorry, Sauber,

but it is getting a nuisance to have the code appended in a URL and I, that I am willing to help you, have to download this stuff, not without getting through some advertisements first.

>>> Just copy and paste your code into your posting, addd
Code:
 at the end of the tags and it should be fairly easy to assist you.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
PROGRAM read_file2
!#1 ON FINAL FOR INPUT AND INPUT 1
! Purpose:
! To illustrate how to read an unknown number of values from
! an input data file, detecting both any formatting errors and
! the end of file.
!
IMPLICIT NONE

! Data dictionary: declare variable types, definitions, & units
CHARACTER(len=20) :: filename ! Name of file to open
CHARACTER(len=80) :: msg ! Status message
INTEGER :: nvals = 0 ! Number of values read in
INTEGER :: status ! I/O status
REAL, DIMENSION (7,2) :: value ! The real value read in

! Get the file name, and echo it back to the user.
WRITE (*,*) 'Please enter input file name: '
READ (*,*) filename
WRITE (*,1000) filename
1000 FORMAT (' ','The input file name is: ', A)

! Open the file, and check for errors on open.
OPEN (UNIT=3, FILE=filename, STATUS='OLD', ACTION='READ', &
IOSTAT=status,IOMSG=msg )
openif: IF ( status == 0 ) THEN

! OPEN was ok. Read values.
readloop: DO
READ (3,*,IOSTAT=status) value ! Get next value
IF ( status /= 0 ) EXIT ! EXIT if not valid.
nvals = nvals + 1 ! Valid: increase count
WRITE (*,1010) nvals, value:),1), value:),2) ! Echo to screen
1010 FORMAT (' ','Line ', I6, ': Value1 = ',F10.4, ' Value2 = ', F10.4, ' ') !FIX this line
END DO readloop

! The WHILE loop has terminated. Was it because of a READ
! error or because of the end of the input file?
readif: IF ( status > 0 ) THEN ! a READ error occurred. Tell user.

WRITE (*,1020) nvals + 1
1020 FORMAT ('0','An error occurred reading line ', I6)

ELSE ! the end of the data was reached. Tell user.

WRITE (*,1030) nvals
1030 FORMAT ('0','End of file reached. There were ', I6, &
' values in the file.')
END IF readif

ELSE openif
WRITE (*,*) TRIM(msg)
END IF openif

! Close file
CLOSE ( UNIT=3 )

END PROGRAM read_file2
 
... and where did this error occur ?

But anyway, the format 1010 does not match the output list of the corresponding write statement. You specify 15 numbers to be written, one integer and 14 reals. Your format specifies one integer and two real values only.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
sorry, don't have time to compile and test, but:

what exactly is the READ statement doing? It is inside a LOOP, yet, the variable being passed to the READ is an entire array...will it try to read the entire file in a single shot? and if so, why is inside a LOOP statement?

Maybe sauber meant to
nvals=nvals+1
READ(3,*) value(nvals,1), value(nval,2)

Then, there is something wrong with the write statement, too...it is passing two vectors (the ':' does that). If the desire is to write one row at a time, then such row's index needs to be explicitly indicated.

gotta go

 
1010 FORMAT (' ','Line ', I6, ': Value1 = ',F10.4, ' Value2 = ', F10.4, ' ') !FIX this line

is where i am getting the error
 
Salgerman,

:) the two of us again :)

I think it is pretty clear what Sauber wants to do. Within this loop he just wants to read one line from his inputfile and wants to write this line to his screen.

But as he is taking fortran classes I thought just a hint on the write and the not matching format would be enough.

Norbert



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Hi, gummibaer:

You must be more patient than I...

I just feel like Sauber did not try much at all...just wrote something, didn't run, asked for help. :-(

Sauber:
Basic programming hints:
1.- Baby steps...baby steps!
2.- Don't do too much in one shot (without re-compiling, running and seeing what you get)
3.- Read and Write, read and write...but make sure that you write the same thing you read...in your case, you are reading 'value' (no indices of any kind) but you are attempting to write value:),1) and value:),2)....that's just not the same thing...

So, first read 'value' and then write 'value' and see what you get...once you know how much data you are reading at a time, then, you will have a better idea on how to proceed to write it.


gsal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top