- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
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?
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
James
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
![](/skins/images/D3C0B914909A6564BBB97F4AD1ED1973/responsive_peak/images/icon_anonymous_message.png)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page