Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] String variable for output filenames

  1. Oct 10, 2012 #1
    This is the troublesome part
    Code (Text):
    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, im just cutting it off, since those are the relevant parameters.
    Code (Text):
    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.
     
  2. jcsd
  3. Oct 10, 2012 #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)
     
  4. Oct 10, 2012 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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)
     
  5. Oct 11, 2012 #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 (Text):
    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!)
     
  6. Oct 11, 2012 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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!
     
  7. Oct 11, 2012 #6

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] String variable for output filenames
Loading...