numbers of rows in a file


gerald
05-25-2008, 05:09 PM
Hello everybody me again I need some help here.

I need program that figures out the number of rows in a file. Here is what I have done so far:

PROGRAM TEST1
REAL x
INTEGER ICOUNT
ICOUNT = 0
OPEN(10,file='old.txt',status='old')
OPEN(11,file='new.txt',status='new')
DO
READ(10,*)x
ICOUNT = ICOUNT+1
END DO
WRITE(11,*) ICOUNT
END PROGRAM TEST1


I thought this program should work but it is not writing the number ICOUNT in file 11 'new', and I am getting a end-of-file error.
The input file looks like this:

1
2
3
45
123
34
2
12
etc...

Thanks,

gerald

davekw7x
05-25-2008, 05:29 PM
I am getting a end-of-file error.
You told it to keep reading forever. When it reaches end-of-file, it can't read any more, so it bails out.


PROGRAM TEST1
REAL x
INTEGER ICOUNT

ICOUNT = 0
OPEN(10,file='old.txt',status='old')
OPEN(11,file='new.txt',status='new')
DO
READ(10,*, END=999) x
ICOUNT = ICOUNT+1
WRITE(*,'(I5,": x = ", F6.2)') ICOUNT, x
END DO
999 CONTINUE
WRITE(*,*)
WRITE(*, '("ICOUNT = ", I5)') ICOUNT
WRITE(11,'("ICOUNT = ", I5)') ICOUNT

END PROGRAM TEST1



Output with the input data that you showed:


1: x = 1.00
2: x = 2.00
3: x = 3.00
4: x = 45.00
5: x = 123.00
6: x = 34.00
7: x = 2.00
8: x = 12.00

ICOUNT = 8


Contents of new.txt after the run:

ICOUNT = 8


I respectfully suggest that you get a book or other resource (class notes, on-line reference, whatever...) and read about Fortran file I/O.

Regards,

Dave

gerald
05-25-2008, 05:40 PM
I respectfully suggest that you get a book or other resource (class notes, on-line reference, whatever...) and read about Fortran file I/O.


Any suggestions?

Thanks,

gerald

davekw7x
05-25-2008, 06:03 PM
Any suggestions?

If it is for a course, what are the prerequisites? Does the course or any of its prerequisites have textbooks in your library or bookstore?

Or... just poke around on the web until you find something that makes sense to you. Different people have different ways of learning (we all have our little quirks).

Since your code is in the style of Fortran 77, you might start with Fortran 77 tutorials. (Just about everything there is still valid for later versions.)
For example: http://www.stanford.edu/class/me200c/tutorial_77/

For all that and some topics in Fortran 90: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html

You do have a search engine, right? Browse a little.

I mean, it's always OK to ask, but ...

Regards,

Dave