Solving Fortran Input File Problem - B

  • Context: Fortran 
  • Thread starter Thread starter Nrets
  • Start date Start date
  • Tags Tags
    File Fortran Input
Click For Summary
SUMMARY

The discussion centers on a Fortran programming challenge regarding the management of key parameters and variable declarations. The user seeks to move key parameters from the type declaration section into an external input file, but faces a structural limitation where global variables cannot be defined until after the input file is opened. Participants suggest that using allocatable arrays may provide a solution, allowing for dynamic memory allocation after reading the necessary parameters from the input file.

PREREQUISITES
  • Understanding of Fortran programming language syntax
  • Knowledge of allocatable arrays in Fortran
  • Familiarity with file I/O operations in Fortran
  • Experience with variable scope and declaration in Fortran
NEXT STEPS
  • Research the use of allocatable arrays in Fortran for dynamic memory management
  • Learn about Fortran file I/O functions, specifically the OPEN and READ statements
  • Explore best practices for organizing parameters in Fortran programs
  • Investigate the implications of variable scope in Fortran, particularly in relation to global variables
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those dealing with complex variable management and file I/O operations. It is also relevant for programmers looking to optimize their code structure for better parameter handling.

Nrets
Messages
1
Reaction score
0
Hey guys,

I am wondering if anyone could help me with a simple problem.

I want to remove key parameters from the "type declaration/parameter" portion of my code and into an external input file for easy access. The problem is, many of my variables (such as arrays) can't be defined until these key parameters are known. However, the program structure disallows a function like "open" in or before the array declarations. In other words, I can only open an input file after I have defined all my global variables. But I can't define all my global variables without opening the input file.

There has to be a solution, yes? Any help would be appreciated.

- B
 
Technology news on Phys.org
Sorry, I did not understand how you could not simply put some code like

Code:
Open (Unit=1, File='File.dat')
Read(1,*)var1
Read(1,*)var2
.
.
.
Read(1,*)varn
Close (Unit=1)

Mainly because you already use some Write\Reads, if I'm guessing right. Maybe some allocatable array? Could you show this bit of code?
 

Similar threads

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