9

Currently, I am using shell command line calls from my fortran program using non-standard SYSTEM intrinsic routine (similar to Fortran 2008 EXECUTE_COMMAND_LINE intrinsic):

CALL SYSTEM(commandStr)

where commandStr is a character string containing the shell command I want to execute. At the moment, I am not aware of a direct way to return the output of commandStr, but only its return status. So, what I am doing now is writing output into a file, and then reading the file from within the Fortran program. Example:

CALL SYSTEM('sed ''s/,//g'' myFile > dummyFile')

if I want to remove commas from myFile. I then use OPEN and READ to get contents of dummyFile.

This works just fine, however I am concerned about writing/reading files from disk, especially if I was doing this within a long loop, and if commandStr output was big. Is there a way to re-direct commandStr output into a memory buffer (not hard disk) which I could access from my Fortran program directly (maybe through a specific UNIT number)?

6
  • If the output was big, wouldn't it make more sense TO store it to file? Commented Oct 20, 2011 at 19:05
  • I guess it would not make a difference for the code - the only part I worry here is frequent I/O to and from disk affecting program efficiency. Is there a reason it would be better to store it to file, something that I am missing? Commented Oct 20, 2011 at 19:10
  • 1
    Well, from my viewpoint it's just a practicality issue. If it's not a lot of data, storing it to a file won't matter. It it is a lot of data, and I'm reusing it in the current running program, the last place where I want it is the memory. I need memory to store the results of "parsing" that file, and then to do something with those. This could've probably been better explained but I think you get the idea. Commented Oct 20, 2011 at 19:37
  • 2
    I sort of get the feeling you're trying to solve a problem you don't have. Commented Oct 20, 2011 at 21:10
  • @eriktous I agree, what I do now works, but the question still stands, is there a different way to do it? Commented Oct 20, 2011 at 21:28

1 Answer 1

1

If this is in a POSIX environment, the library function popen() might also be available.

iunit = popen ('sed ''s/,//g'' myFile', 'r')

Look at the documentation for your Fortran environment since I'm not sure of the semantics for connecting the i/o to Fortran. If it is like the C runtime library, the file connection also needs a special function to close it, pclose().

Sign up to request clarification or add additional context in comments.

1 Comment

You could use the Posix stuff through the ISO_C_Binding, there is a project concerned with that at: sourceforge.net/projects/fortranposix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.