Fortran Problem in Extracting Numercial Values in fortran

  • Thread starter Thread starter equillibrium
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion revolves around an issue with reading numerical values from a text file using Fortran with an IBM compiler. The user is experiencing a problem where the program skips the loop between the 'Beta' section and the 'END' marker, leading to errors in reading the data. The input file contains both text and numerical data, and the user has tried using a 'do while' loop but encounters read statement errors due to unexpected characters in the input. A suggested solution involves reading the entire file as text first to count the data entries, then rereading the file while skipping non-numeric lines. The user successfully implemented this approach, resulting in a functional program. Additionally, there was a recommendation to avoid using 'goto' statements for cleaner code structure.
equillibrium
Messages
2
Reaction score
0
Hello all,I have a small doubt. I use F95/90 and IBM compiler.This is a portion of my text file
-----------------------
Alpha Singles Amplitudes
15 3 23 4 -0.186952
15 3 26 4 0.599918
15 3 31 4 0.105048
15 3 23 4 0.186952
Beta Singles Amplitudes
15 3 23 4 0.186952
15 3 26 4 -0.599918
15 3 31 4 -0.105048
15 3 23 4 -0.186952
END
---------------------
I am trying to extract the numerical values from the attached file 1.txt . and I am facing a strange error in the output which I cannot understand. Every time I execute the program it skips the loop between 'Beta' and 'END'. I am trying to read and store the values.
The number of lines inside the Alpha- and beta loop is not fixed. So a simple 'do loop' is of no use to me. I tried the 'do while' but it gives the output as :
-------------------------------------------------------------------------------------
AS 15 3 23 4 -.186952
AS 15 3 26 4 .599918
AS 15 3 31 4 .105048
AS 15 3 23 4 .186952
1525-097 A READ statement using decimal base input found the invalid digit 'a' in the input file. The program will recover by assuming a zero in its place.
1525-097 A READ statement using decimal base input found the invalid digit 'e' in the input file. The program will recover by assuming a zero in its place.
1525-097 A READ statement using decimal base input found the invalid digit 'p' in the input file. The program will recover by assuming a zero in its place.
1525-097 A READ statement using decimal base input found the invalid digit 'l' in the input file. The program will recover by assuming a zero in its place.
This is the end
--------------------------------------------------------------------------------------
Any suggestions would of of great help. I coudn't find the error in logic in my code.I have attached the code (Text.txt).

Thanks in advance
 

Attachments

Last edited by a moderator:
Technology news on Phys.org
At one point, you try to execute the statement

160 read(10,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r

while the input file contains

Beta Singles Amplitudes

obviously, your program does not like that.

The inelegant solution that comes to my mind is to first read the entire file as text, counting how many AS and BS data there is. Then reread as numbers, skipping text lines. There is probably a more clever way to it, but that should do it.

And please, please remove the gotos! Having a do while that includes a goto is particularly ugly: at least use an if-then
 
Thank you Dr. Claude for the suggestion.. I used that idea and now it seems to work very nicely for the entire data set. :) :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
3K
Replies
22
Views
5K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
16
Views
2K
Replies
5
Views
2K
Replies
13
Views
5K
Replies
3
Views
4K
Back
Top