[Fortran] String variable for output filenames

In summary: In this conversation, the problem was that the programmer was attempting to use index zero, which is not defined in Fortran. By properly declaring the length of the string and using the trim function, the issue was resolved. It is important to understand the capabilities and limitations of a programming language in order to effectively use it.
  • #1
panzerlol
3
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
  • #2
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)
 
  • #3
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)
 
  • #4
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!)
 
  • #5
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!
 
  • #6
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.
 

Related to [Fortran] String variable for output filenames

1. What is a string variable for output filenames in Fortran?

A string variable for output filenames in Fortran is a data type that is used to store a sequence of characters, such as letters, numbers, and symbols. It can be used to represent the name of a file that is being created or read by a Fortran program, and can be manipulated to create dynamic and meaningful filenames.

2. How do I declare and initialize a string variable for output filenames in Fortran?

To declare and initialize a string variable for output filenames in Fortran, you can use the CHARACTER data type. For example, you can declare a string variable named "filename" and initialize it to "output.txt" by using the following code: CHARACTER(10) :: filename = 'output.txt'. This will create a string variable that can hold up to 10 characters.

3. Can a string variable for output filenames be used in a WRITE statement?

Yes, a string variable for output filenames can be used in a WRITE statement in Fortran. This allows you to dynamically create output filenames based on certain conditions or user input. You can use the string variable in the file name field of the WRITE statement, such as WRITE(filename,*) 'Hello World'. This will write the string "Hello World" to the file named "output.txt" in the above example.

4. How can I manipulate a string variable for output filenames in Fortran?

There are several built-in functions and operations that can be used to manipulate a string variable for output filenames in Fortran. These include concatenation (using the // operator), substring extraction (using the substr function), and conversion to uppercase or lowercase (using the ucase or lcase functions). These operations can be used to dynamically create filenames based on user input or system variables.

5. Are there any limitations to using string variables for output filenames in Fortran?

While string variables for output filenames in Fortran are versatile and useful, there are some limitations to keep in mind. The maximum length of a string variable is limited by the compiler, so you may not be able to create extremely long filenames. Additionally, some special characters may not be allowed in filenames, so you may need to check for these and handle them appropriately when manipulating the string variable.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
12
Views
15K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
17K
Back
Top