[Fortran] String variable for output filenames

Click For Summary

Discussion Overview

The discussion revolves around issues related to generating output filenames in Fortran, specifically focusing on string manipulation and file handling. Participants explore the challenges faced when constructing a filename from a character string and the implications of array indexing in Fortran.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue where the constructed filename ends up being incomplete, resulting in an output file named only "ElonL".
  • Another participant points out that using fileNameSuffix(0:25) is problematic because Fortran arrays start at index 1 unless specified otherwise.
  • A third participant suggests using trim(fileNameSuffix) instead of fileNameSuffix(0:25) to avoid including blank spaces in the filename.
  • A later reply indicates that the participant resolved their issue by applying trim and correcting their indexing, noting that previous formatting might have caused problems.
  • One participant reflects on the idea of programming languages "understanding" user intent, sharing an anecdote about user input that led to unexpected output, emphasizing the need for clarity in programming.
  • Another participant reiterates the importance of understanding the capabilities of the programming language to better communicate intent.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correct indexing and string manipulation in Fortran, but there are differing views on the concept of programming languages "understanding" user intent.

Contextual Notes

Participants discuss limitations related to array indexing and string handling in Fortran, highlighting the need for careful attention to these details when constructing filenames.

panzerlol
Messages
3
Reaction score
0
This is the troublesome part
Code:
eLeftOutFile = 'data/ElonL'//fileNameSuffix(0:25)//'.dat'
OPEN(754,FILE=eLeftOutFile,STATUS='REPLACE',ACCESS='SEQUENTIAL')

First off, fileNameSuffix is a CHARACTER(len=40) just to be safe, I am just cutting it off, since those are the relevant parameters.
Code:
WRITE(*,*) fileNameSuffix
yields the correct 08V077K1000fsSample1ps, so that string works fine.

As you see I'm trying to make a string reading "data/ElonL08V077K1000fsSample1ps.dat"
representing the path and filename I want to generate as output.

However, the output file ends up JUST being named ElonL. If i remove ElonL and make eLeftOutFile look like 'data/08V077K1000fsSample1ps.dat'
then I get forrtl error SEVERE 30. It looks as if the whole string is ignored and the program halts because I try to generate a file with no name in the data/ folder.

First time poster, so please ask any questions if I've been unclear.
 
Technology news on Phys.org
The problem is that you are attempting to use fileNameSuffix(0:25), but index zero is not defined...arrays in Fortran start at 1 if not explicitly delcared with starting and ending indeces.

By simply saying length 40, you string is defined from 1 to 40, both ends inclusive.

In fortran, it is also possible to declare an array like so: real myarray(-5:5)
 
What gsal said, plus the fact that in your example "data/ElonL08V077K1000fsSample1ps.dat" the string isn't 25 characters long, so you will probably end up with a filename containing some blanks.

Maybe you should use trim(fileNameSuffix) not fileNameSuffix(0:25)
 
Thank you all so much.

Critique to fortran aside (and how I think any good programming language should "understand" what i wanted it to do, it worked great.

In short, all I did was run trim(..) on the filename suffix.

While generating the suffix string, i had used
Code:
WRITE( tempstring, '(i2.2)' ) INT(ABS(VL-VR)) 
		fileNameSuffix = tempstring([B][U]1[/U][/B]:2)//'V'

I also went over those parts and corrected that index. (the bold and underlined 1 was zero in all my previous statements.) While the resulting string LOOKED ok, I guess may have had some bad formatting leading Fortran to refuse using it for a filename.

(This is just me stating a lesson for other potential strugglers, thanks again for helping me!)
 
panzerlol said:
Critique to fortran aside (and how I think any good programming language should "understand" what i wanted it to do, it worked great.

A programming language that "understands what you want" might seem like a nice idea, but that's not how programming languages wiork. You just have to get used to the idea that computers do what you say, not what you mean.

Actually "do what you mean" can have problems as well. I once wrote a program that required a lot of numerical quantities typed in by users, and we got a request to make all the typed input "understand" arithmetiic operations, so for example if the user knew the diameter of something was 12.345 inches and the program wanted the radius in mm, he/she could just type 12.345*25.4/2 instead of using a calculator. This also had the good side-effect that the user would probably recognize the value 12.345 at a later date, instead of wondering where the number 156.7815 had come from.

But the day after we released the change, we got a complaint from somebody working on part number 1234-002 who complained that all his output was now titled "part number 1232". Some you win, some you lose!
 
panzerlol said:
Critique to fortran aside (and how I think any good programming language should "understand" what i wanted it to do, it worked great.

AlephZero said:
A programming language that "understands what you want" might seem like a nice idea, but that's not how programming languages wiork. You just have to get used to the idea that computers do what you say, not what you mean.

A programming language will better understand what you want it to do, if you have a clear understanding of what the language is able to do.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 12 ·
Replies
12
Views
16K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
18K