Problem in Extracting Numercial Values in fortran

  • Fortran
  • Thread starter equillibrium
  • Start date
  • Tags
    Fortran
In summary, you are trying to extract the numerical values from a text file. Every time you execute the statement, it skips the loop between 'Beta' and 'END'. You tried the 'do while' but it gives the output as:
  • #1
equillibrium
2
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

  • 1.txt
    378 bytes · Views: 443
  • Test.txt
    1.3 KB · Views: 430
Last edited by a moderator:
Technology news on Phys.org
  • #2
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
 
  • #3
Thank you Dr. Claude for the suggestion.. I used that idea and now it seems to work very nicely for the entire data set. :) :)
 

1. What is the main problem in extracting numerical values in Fortran?

The main problem in extracting numerical values in Fortran is that the language is not as user-friendly as other modern programming languages. It requires a thorough understanding of its syntax and a precise approach to data manipulation in order to successfully extract numerical values.

2. How do you extract numerical values from a Fortran program?

To extract numerical values from a Fortran program, you need to first define the variables that will store the numerical data. Then, you can use the READ statement to read in the data from a file or the keyboard. Finally, you can use the WRITE statement to output the numerical values to the desired location.

3. What is the difference between extracting numerical values in Fortran and other programming languages?

The main difference between extracting numerical values in Fortran and other programming languages is the syntax. Fortran uses a fixed-format syntax, where each line of code must start in a specific column, making it more difficult to read and understand for beginners. Additionally, Fortran uses a different approach to data manipulation, which may require more steps and precision.

4. Can you extract numerical values from a Fortran program without using files?

Yes, it is possible to extract numerical values from a Fortran program without using files. You can use the READ statement to read in data from the keyboard and the WRITE statement to output the values to the screen. However, using files can make the process more organized and efficient.

5. Are there any tips for successfully extracting numerical values in Fortran?

Some tips for successfully extracting numerical values in Fortran include closely following the syntax rules, using descriptive variable names, and carefully checking for errors. It can also be helpful to break down the extraction process into smaller steps and test each one separately. Additionally, utilizing available resources and seeking help from experienced Fortran programmers can aid in solving any issues that may arise.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
322
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
19
Views
982
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top