Fortran question, read from file

In summary, the programmer is having trouble keeping one coordinate constant while varying the rest. He needs to learn how to use fromatted read statements.
  • #1
_Andreas
144
1
Hello. I'm completely new to programming, and I'm having some troubles with a Fortran program I'm writing.

I have an input file containing three columns, where each column contains a couple of cartesian coordinates (column 1 contains various x coordinates, and so on). What I would like my program to do is to keep one (or two) coordinate constant, while varying the rest. The relevant part of my code looks like this:

open(11, file="filnamn")
do i=1,N
read(11, *) 1, 2, 3
...

Here all the columns are read. But if I want the program to read only column 2 and 3 I can't write

read(11,*) 2, 3

since this of course would make it read column 1 and 2. So my question is: How can I make the program read a specific column?
 
Technology news on Phys.org
  • #2
You need to learn how to use fromatted read statements. What you are using now is free format reading.
Example:
read(11,100)x_coordinate,y_coordinate
100 format(3x,F12.9,3x,F12.9)

this will skip 3 spaces and read a real number in the format nn.nnnnnnnnn then skip another 3 spaces and read another real number in the format nn.nnnnnnnnn
Take some time to learn different formats
 
  • #3
Just read all three each time anytimes, but keep the previous values stored
Code:
DO n=1,nPts
 READ(11,*) A(n),B(n),C(n)

 IF (whatever) THEN
   A(n) = Alast
 END IF

 Alast = A(n)
 Blast = B(n)
 Clast = C(n)

END DO
Something like that
 
  • #4
Thanks for trying to help me, guys. I found out that I was on the wrong track, though.
 
  • #5
I program in c++ however I have used fortran. If you have a pointer with the
address of the starting data ( your mem mapped data) write a struct which
coresponds to the data i.e. struct{
 
  • #6
I program in c++ however I have used fortran. If you have a pointer with the
address of the starting data ( your mem mapped data) write a struct which
coresponds to the data i.e. struct memmap {
int a
float b
double int
+,
ect...
}
Then point the struct to the starting address and if you created the struct to
match your data your finished. I don't know the fortran syntax for a struct of hand.
however you should be able to find that. Trick is declaring it a pointer.
If all goes wrong write a c routine then link it via a call from fortran. You can also
write an assembler interface between a C routine and fortran

Geo.
 

1. What is Fortran?

Fortran is a programming language that was developed for scientific and engineering applications. It was first created in the 1950s and is still widely used today for its performance and efficiency in handling complex mathematical calculations.

2. How do I read from a file in Fortran?

To read from a file in Fortran, you can use the READ statement followed by the name of the file and the variable where you want to store the data. You can also specify the format of the data in the READ statement.

3. Can Fortran read different types of files?

Yes, Fortran can read both formatted and unformatted files. Formatted files have a specific structure with data separated by spaces or commas, while unformatted files store data in its raw binary form.

4. How do I handle errors when reading from a file in Fortran?

You can use the IOSTAT parameter in the READ statement to check for errors while reading from a file. If an error occurs, the value of IOSTAT will be non-zero, and you can handle the error accordingly in your code.

5. Are there any other useful Fortran libraries or functions for reading from files?

Yes, there are several useful libraries and functions available in Fortran for reading from files, such as the INQUIRE statement, the GETARG function for reading command line arguments, and the SYSTEM function for executing system commands to read from files.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
915
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
963
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top