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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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