Topic: Where are Read/Write files live?

I understand the Read and write statements as far as how they specify the keyboard/monitor or an external file as the in/out recipient. But where do you specify where - what folder - the file is located? Is it a given that the file you are trying to read or write to is in the same location as the executable? Similarily, I wonder about “Print” - does it go so the system default printer with it’s default parameters. I’m so use do dialog boxes to specify those locations, which printer (with attributes like 2 sided, etc.).

So I see how it can read something like “MyData.txt” but where does MyData.txt live?

Re: Where are Read/Write files live?

I responded to this via email too a few moments ago.  Basically, you'll want to use OPEN statements to actually access files.  These files, by default, will be opened in the current working directory of your program.  In Simply Fortran, this is the same directory as the executable and/or project file is located.  This can be changed, but that's the default.

The Print statement is merely a shortcut for writing to the screen.  The following are equivalent:

WRITE(*,*) "I am writing to the screen"

and

PRINT *, "I am writing to the screen"

The Print statement can't be used to write to files, only the standard output.

Writing to a printer is far too complex, and there is no quick and easy way to do so.  It would require non-portable operating system calls.  Furthermore, modern printers have no good way to deal with Fortran's "line-by-line" output style any longer.  It's best to write to a file and subsequently print that file.

Jeff Armstrong
Approximatrix, LLC

Re: Where are Read/Write files live?

I began using Simply Fortran for the first time today and love it!  Many thanks, Jeff, for your support.  It would work best for me if the default location of file i/o were the same as that of the executable.  But I see it's always my home directory, regardless of where the executable is located.  Is this a change from the below post in 2020?  And if so, can I redirect it, i.e., w/o including a path name in every open?

jeff wrote:

I responded to this via email too a few moments ago.  Basically, you'll want to use OPEN statements to actually access files.  These files, by default, will be opened in the current working directory of your program.  In Simply Fortran, this is the same directory as the executable and/or project file is located.  This can be changed, but that's the default.

The Print statement is merely a shortcut for writing to the screen.  The following are equivalent:

WRITE(*,*) "I am writing to the screen"

and

PRINT *, "I am writing to the screen"

The Print statement can't be used to write to files, only the standard output.

Writing to a printer is far too complex, and there is no quick and easy way to do so.  It would require non-portable operating system calls.  Furthermore, modern printers have no good way to deal with Fortran's "line-by-line" output style any longer.  It's best to write to a file and subsequently print that file.

Re: Where are Read/Write files live?

m.banner wrote:

It would work best for me if the default location of file i/o were the same as that of the executable.  But I see it's always my home directory, regardless of where the executable is located.  Is this a change from the below post in 2020?  And if so, can I redirect it, i.e., w/o including a path name in every open?

When you compile an executable, that executable is now subject to the rules and whims of the operating system,  By default, an executable will access files, when no full, absolute path is provided, in the current working directory. 

In Simply Fortran, when you launch an executable that was built as a project, the current working directory is set to the directory where the project file is saved (not the source file).  If you haven't changed any Launch options, the executable will be run from the directory where the project file was saved.

If you're using the Compile and Run Current File feature in the Build menu, the resulting executable should be located in and run from the same directory where the source file is found.  Even if you have this file in a project, that menu option will cause the working directory to be the same as the source directory.

I'm not sure what the circumstances are in your case, though.  Where exactly is the executable being built?  And how are you building the executable, via project?

Jeff Armstrong
Approximatrix, LLC

Re: Where are Read/Write files live?

Ok, I see my problem.  And I may have to change the way I'm used to working?  I have indeed been using the project build function in the IDE, but.. rather than running the executable from a terminal command line, I've been just double clicking it.  So my "working" directory remains my "home" directory, thus that's where my output files are appearing.

In the past, most recently working with Absoft Fortran and their IDE, after developing a program, I could create a directory anywhere, containing my input files, then move or copy my executable into that directory and double click.  The output files would appear in that same directory.  Any possibility, maybe in the make/build process, to imitate this behavior?

The advantage was, I could have data files with the same names but kept in separate folders, e.g., "case studies," and just move or copy my executable into each folder and double click.  Then all i/o would be contained in that folder.

Re: Where are Read/Write files live?

My understanding was that Absoft did some tricks with "terminal" programs on macOS to avoid the behavior you're seeing.  Our compiler is just generating a basic macOS executable that doesn't treat macOS as anything special; however, macOS's GUI is a little "odd" as far as UNIX compatibility goes. 

The solution if you want to double-click in Finder is either:

1. Create a shell script that changes the working directory and then calls your Fortran program.  You could then double-click that.

or:

2. Add a bit of Fortran to change the directory:

character(128)::exe
integer::final_slash

    call get_command_argument(0, exe)
    final_slash = index(exe, "/", back=.true.)
    if(final_slash .ge. 1) then
        exe(final_slash:128) = ' '
        call chdir(exe)
    end if

which should change the directory to the directory where the executable exists.  I realize neither is ideal, though.

Jeff Armstrong
Approximatrix, LLC

Re: Where are Read/Write files live?

Thanks Jeff.  I'm ready to close the topic. Getting familiar with Simply Fortran now, and the file location issue is not really a problem.  I just open a Terminal window, cd to the particular directory with my files, and ./ the program.

Being strictly an end-user of Fortran, I only want to know enough unix to get by.  Have to admit I was spoiled in the 80s by Language Systems Fortran using Apple's MPW and the SADE debugger.  At the time, it was light-years ahead of my co-workers at Boeing who were struggling with the DOS prompt on their PCs.  And a huge leap ahead of coding sheets, punched cards and OTC batch processing on the mainframe IBM, CDC and Cray.  There, one typo meant next day turnaround.   Back then, I studied the three volumes of "Inside MacIntosh" and built Fortran interfaces to many of Apple's primitives, especially graphics, so I could generate and interact in real time with visual representations of my algorithms.  Sadly, that all went away (for me) with "new and improved" systems.