Fortran question, read from file

  • Context: Fortran 
  • Thread starter Thread starter _Andreas
  • Start date Start date
  • Tags Tags
    File Fortran
Click For Summary

Discussion Overview

The discussion revolves around reading specific columns from a file in Fortran, particularly focusing on how to manage input data containing multiple columns of coordinates. Participants explore various methods to achieve this while addressing the challenges faced by a beginner in programming.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about reading specific columns from a file in Fortran and seeks guidance on how to keep certain coordinates constant while varying others.
  • Another participant suggests using formatted read statements to skip columns and read specific data, emphasizing the need to learn different formats.
  • Another approach proposed involves reading all columns each time but storing previous values to manage the data effectively.
  • One participant shares insights from their experience with C++ and Fortran, discussing the use of structures to map data and suggesting the possibility of linking C routines with Fortran.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to handle the reading of specific columns, with no consensus reached on the best approach. Various methods are suggested, but the discussion remains unresolved regarding the most effective solution.

Contextual Notes

Some participants reference specific programming techniques and structures without providing complete solutions or clarifying the Fortran syntax, which may limit understanding for those unfamiliar with the language.

_Andreas
Messages
141
Reaction score
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
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
 
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
 
Thanks for trying to help me, guys. I found out that I was on the wrong track, though.
 
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{
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
7K