Fortran [Fortran] String variable for output filenames

AI Thread Summary
The discussion revolves around a Fortran programming issue related to file naming and string manipulation. The user is attempting to create a file path and name using a string variable, but encounters problems due to incorrect indexing and string length handling. Specifically, using fileNameSuffix(0:25) leads to errors because Fortran arrays start at index 1 unless specified otherwise. The solution involves using the TRIM function to remove any trailing spaces from the string, ensuring the filename is correctly formatted. The conversation also touches on broader programming principles, emphasizing that programming languages execute commands literally rather than intuitively interpreting user intent. This highlights the importance of understanding the language's syntax and behavior to avoid common pitfalls.
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
6
Views
2K
Replies
3
Views
3K
Replies
6
Views
3K
Replies
12
Views
15K
Replies
4
Views
9K
Replies
10
Views
4K
Replies
4
Views
2K
Replies
8
Views
18K
Back
Top