Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26731 Discussions

Sharing files between C++ and Intel Fortran

Ibrahim_K_
New Contributor I
189 Views

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
1 Solution
Steve_Lionel
Black Belt Retired Employee
189 Views

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.

View solution in original post

11 Replies
Steve_Lionel
Black Belt Retired Employee
190 Views

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.

View solution in original post

Ibrahim_K_
New Contributor I
189 Views

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

 

A__King
New Contributor III
189 Views

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.

Steve_Lionel
Black Belt Retired Employee
189 Views

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.

LRaim
New Contributor I
189 Views

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 

Steve_Lionel
Black Belt Retired Employee
189 Views

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. 

IanH
Black Belt
189 Views

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).

Steve_Lionel
Black Belt Retired Employee
189 Views

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.

LRaim
New Contributor I
189 Views

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. 

Steve_Lionel
Black Belt Retired Employee
189 Views

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.

Ibrahim_K_
New Contributor I
189 Views

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

Reply