- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

I have a .txt file containing 1 column and 101 rows. For example the first 4 rows are as follows:

0.00000E+00

6.28215E+15

1.25581E+16

1.88217E+16

I want to read the values in and store them for use in calculations.

How would I tackle this?

Thanks.

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

This is a very basic question. Is it a homework assignment?

Any textbook on Fortran ought to give you the clues for solving this "problem". But here are some clues: use an array and the open and read statements

- Tags:
- This is

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

It’s just an example I’ve been asked to do but can’t figure it out.

I’ve managed to open the file and read it by using:

read (10,*)a,b

but that only reads the first 2 lines. Is there a way to read all lines and assign all values to something without typing out 101 different variables?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Ah, that explains it :). Well, certainly there is. There is a wide variety of possibilities actually.

Since you are doing an introductory course, try the following (without giving you a ready-made solution by the way):

- Declare a real array of size 101, call it, for instance, mydata
- Use the read statement to read in the entire array
- If you want to know a single value, you can do so by:

`write(*,*) mydata(10) ! Print the tenth element`

This is a straightforward way to read the data, but it assumes:

- you know the number of data beforehand
- the file contains the data in the form you specified (one value per line) or as a number of values on a line, as long as there are spaces between the data and no text etc. in between

If these conditions are not met, you will get a run-time error.

If you need to make your program more robust or more flexible, there is more work involved, but the above sketch should get you going.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Thanks very much I'll give this a go and see how it goes.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

My code is as follows:

program list

integer :: err

character(256) :: err_msg

real, dimension(101) :: flux

open(10, file='flux.txt', status='old', iostat=err, iomsg=err_msg)

if(err /= 0) then

write (*,*) 'Error ', trim(err_msg)

stop

endif

read (10,*)

print(*,*) flux(10)

end

but this error message is showing:

print(*,*) flux(10)

1

Error: Syntax error in PRINT statement at (1)

What am I doing wrong?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Can you please share here just what kind on instructional material (textbook(s) and/or lecture notes and/or blogs or videos?) do you have for this introductory course in Fortran? Have you gone through such material with a thoroughness the instructor would expect of anyone taking the course? Because you're asking the most basic questions. The material you have should show the following is invalid:

`print (*,*) !<-- Invalid syntax`

Re: each statements in Fortran (or any other learning for that matter), why not adopt the RTFM approach? e.g., consult the information available at your fingertips such as Intel documentation online. Here it is for PRINT statement:

In case you find your instructional material inadequate, you can start with some free stuff online to get you going immediately like the following from UNLV:

http://www.egr.unlv.edu/~ed/fortran.html

Or, you can look into getting your hands on a formal textbook such as this one:

https://www.amazon.com/Fortran-Scientists-Engineers-Stephen-Chapman-ebook/dp/B06XCTY8KR

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Due to Covid everything has been online so the teaching hasn't been great. I have been given 5 PowerPoints each with about 10 slides on so haven't been given much information to go off. I haven't been given access to a manual so I'm sure you can see that a RTFM approach could be quite difficult without a manual.

The manual you have given me here seems excellent though so thank you very much for that it will be a great help. I will go through this today and hopefully that will solve the problems I am having.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Look at your read statement from your first attempt

and compare it to the read statement from your second attempt.

What is missing?

And more important, what is the relationship between the read and what is missing?

Jim Dempsey

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Thanks for your response. I'm about to start working on my code again now so hopefully I can solve my problem.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

As part of your studies, note if you're interested in computing generally and also the upcoming fields in "data sciences" and numerical programming, you can get a book bundle for $12 that includes a good book on "modern Fortran" as well: https://www.humblebundle.com/books/math-for-programmers-manning-publications-books

In terms of readily accessible resources online, do keep in mind the Fortran Wiki site:

http://fortranwiki.org/fortran/show/HomePage

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Ok thanks for this I will make sure to check them out.

The "Introduction to Programming using Fortran 95/2003/2008" pdf you sent me has been brilliant so far I have learnt more from this in the past hour or so than I have across the entire course I am on so thanks a lot for that I would highly recommend it for beginners.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

In addition to the books recommended by FortranFan, Google is your friend.

e.g. Fortran read statement

Jim Dempsey

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

You can download the Intel Fortran manuals - search for the PDF versions, there are simple examples available to anyone with each reference example in the manual. These help more than most textbooks.

You have hit the right place, but to ensure you learn, these guys will make you work for it, but they mean well.

I learnt lisp alone from the reference manual -- I can remember one line took a week to debug.

You are in world class company - so good luck --

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page