Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I

Sharing files between C++ and Intel Fortran

Jump to solution

Friends;

I would like to open files for input and output (not the same file) in C++ and write or read from them in a subroutine linked with a C++ main program. 

I think I can open files in Fortran in a C++/Fortran program using the standard file management functions without messing up the files used by C++ main. But I am suspicious that I will mess up files if I tried to do that with a file already open in C++.

Is there a simple solution to this problem? 

I. Konuk

0 Kudos

Accepted Solutions
Highlighted
Black Belt

You are correct - trying to

Jump to solution

You are correct - trying to open the same file simultaneously in Fortran and C++ will create problems. Don't do this. Call a routine in whichever language opened the file to read or write it.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
11 Replies
Highlighted
Black Belt
1 View

You are correct - trying to

Jump to solution

You are correct - trying to open the same file simultaneously in Fortran and C++ will create problems. Don't do this. Call a routine in whichever language opened the file to read or write it.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
Highlighted
New Contributor I

Steve:

Thank you very much. More I thought about it more I realized how stupid my idea is. In addition to the different data structure created by different compilers, and the data structures and entry points used by different libraries in different operating systems would be a nightmare to synchronize.

Thank you again for your prompt and wise advice. I have started on a different design by moving all my file IO to C++ which will require some work to strip parts of my legacy code.

I. Konuk

 

0 Kudos
Highlighted
New Contributor I

Quote:Steve Lionel (Ret.)

Jump to solution

Steve Lionel (Ret.) (Blackbelt) wrote:

You are correct - trying to open the same file simultaneously in Fortran and C++ will create problems. Don't do this. Call a routine in whichever language opened the file to read or write it.

Steve, since you said "read or write", then what would happen, if the same file is opened on multiple coarray images? Does the Fortran standard say anything about this scenario. I ask, because I have written codes in the past that read the same file simultaneously from multiple images (no writing happens though), and it has so far worked with no problems.

0 Kudos
Highlighted
Black Belt

Prior to Fortran 2018,

Jump to solution

Prior to Fortran 2018, opening the same file in multiple images was not allowed by the standard. In Fortran 2018, it is "processor-dependent", meaning you're not breaking the standard doing so, but the results are unspecified.

I'll note that the standard encourages implementations to "merge" standard output streams from images to image 1, but this is not a requirement. Standard input is preconnected on image 1 only.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
New Contributor I

It is years since I started

Jump to solution

It is years since I started to exchange data between Fortran simulation executables and the C++ windows interface using the same files.

There is nothing which prohibits such use. You have only to apply the correct OPEN parameters to assure the cooperation of the software executables.

Regards 

0 Kudos
Highlighted
Black Belt

There may be "nothing which

Jump to solution

There may be "nothing which prohibits such use", but whether it does what you want is not guaranteed. Having a file opened in two languages at once may be a problem due to buffering and tracking of file position. 

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Black Belt

Post #6 uses plural

Jump to solution

Post #6 uses plural "executables".  Is that because the Fortran and C++ bits are in different executables?  If so, that's a different case to the original post, where there is a single executable, that has both Fortran and C++ code operating on a common file (which is likely to be problematic).

0 Kudos
Highlighted
Black Belt

Either way it's likely to be

Jump to solution

Either way it's likely to be a problem as I don't think Windows shares file positions across processes. I'll note that any sequential write from Fortran truncates the file at the current position.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
New Contributor I

Quote:IanH (Blackbelt) wrote:

Jump to solution

IanH (Blackbelt) wrote:

Post #6 uses plural "executables".  Is that because the Fortran and C++ bits are in different executables?  If so, that's a different case to the original post, where there is a single executable, that has both Fortran and C++ code operating on a common file (which is likely to be problematic).

Yes, the same file is processed at the same time by one executable written in C++ and one in Fortran.

If two pieces of code (C++ and Fortran) are in the same executable, information sharing between the C++ section and the Fortran section  would be more simple.

Files are objects defined by the operating system not by a language. So the language makes available the input/output functions on a file allowed by the operating system. 

0 Kudos
Highlighted
Black Belt

Yes, but the language has its

Jump to solution

Yes, but the language has its own rules of how I/O operations in the language translate to calls to the OS. The language library may build up buffers and write things when it feels it needs to, and a write in one language may not be immediately seen by a read in the other.

It's never a good idea to do mixed-language I/O.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
New Contributor I

I dared and decided to

Jump to solution

I dared and decided to experiment with a partial version of doing I/O (only WRITE) from both parts of C++/Fortran program and I found a trap or bug.

It works mostly if one outputs to separate files not shared by Fortran and C++. However, if one decides to do some fancy thing, C++ gives exception.

The fancy thing I am referring to in my case is that if I decide to redirect std::cout to a file stream, I get exception even I close the file in Fortran before doing so. When I find some time I will do a small example and post it here.

This example validates the advice given by Steve; do not mess I/O in mixed code! I though I share it here.

I. Konuk

0 Kudos