Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28595 Discussions

convert Fortran file unit number to C file point

jq2000
Beginner
1,051 Views
Requirement:
A Fortran program opens a few files, then call a C function to write to the files.

Question:
How can I convert a file unit number in Fortran to a FILE point in C for the same file?

0 Kudos
4 Replies
james1
Beginner
1,051 Views
Perhaps you could say a little more about what you are trying to do. My first reaction is "bad idea, don't do it". Alternatives would be to open the files in Fortran, do whatever it is you need to do in Fortran, then close them and re-open in C. Next would be to simply call C routines to do all the file I/O. Yet another alternative would be to use system services to do the I/O to these sorts of files rather than using the respective language RTLs.

James
0 Kudos
Steven_L_Intel1
Employee
1,051 Views
A Fortran unit in CVF has no correspondence to a C file point. The CVF run-time library calls Win32 API routines directly to do I/O.

Steve
0 Kudos
jq2000
Beginner
1,051 Views
Thank you for your tips. It is very helpful.

My project is to add a new function to a legacy Fortran program. I prefer to use C in order to reuse it in other versions in the future.

Scennario:
The Fortran program
* open a input file and a output file, say ~{!0~}Out_1~{!1~}
loop through all records
* repeat reading, processing data and write some results to ~{!0~}Out_1~{!1~}
* C function ~{!0~}Bridge~{!1~} will be inserted here to make further process, write to ~{!0~}Out_1~{!1~}, also create 2 new output files: ~{!0~}Out_2~{!1~} and ~{!0~}Out_3~{!1~} and write to them.
* output result to ~{!0~}Out_1~{!1~} for each process cycle.
End loop
* Close all files

~{!0~}Out_1~{!1~} is shared by both Fortran and C. Elementyl~{!/~}s 1st recommendation could be the best way to do. Disadvantage is ~{!0~}Out_1~{!1~} has to be opened and closed thousands times in the C function within the Fortran loop.

~{!0~}Out_2~{!1~} and ~{!0~}Out_3~{!1~} can be independent from Fortran, I will try to use static file pointers in C. pass a flag from Fortran to C to indicate 1st call and last call. C opens the files in 1st call and closes it in last call. In other calls, the files remain open status.

I wonder if it is possible to realize: create another 2 C functions: ~{!0~}Open~{!1~} and ~{!0~}Close~{!1~}. Fortran calls ~{!0~}Open~{!1~} before the loop and call ~{!0~}Close~{!1~} after the loop. Addresses of file pointers are passed from ~{!0~}Open~{!1~} to Fortran as integers. Fortran then passes them to ~{!0~}Bridge~{!1~} as integers. ~{!0~}Bridge~{!1~} recovers file pointers from the integer addresses.

Jin
0 Kudos
james1
Beginner
1,051 Views
Whether having many opens/closes in your loop is inefficient depends on what else the program is doing. These opens/closes could be a minor event in the overall scheme in which case it is certainly better than doing something non-standard, like trying to swap file pointers.

It is probably possible to get the underlying file handle, however not with any supported interface. The other problem is each language RTL likely keeps track of the current location within the file that the RTL *thinks* the file system is using, and if you do work from the other language RTL this is asking for corruption.

From you description I personally would just do the file I/O in Fortran unless you are planning to do away with the Fortran code altogether - that is technically the best solution. The actual file I/O cannot be very much code. If you really want to have both languages read/write these files, then do the necessary open and close operations and watch the file format to ensure they are compatible between the two RTLs.

James
0 Kudos
Reply