Fortran: how do you read from a memory-mapped file?

In summary, the conversation is about someone needing help with reading binary data from a memory-mapped file using Fortran. They know how to do it in other languages but are struggling with Fortran. They have managed to open and create a mapview of the file, but are unsure how to read the data and perform pointer arithmetics. They also mention using a WinApi function and the Fortran loc function to solve their problem.
  • #1
Franzy
2
0
Hello everyone!

I have some binary data (list of integer and float values) stored in a memory-mapped file. I need to read this data. I know how to do this in Delphi and C, but Fortran completely stalled me. So far I managed to open my virtual file (OpenFileMapping) and create a mapview (MapViewOfFile). I believe I have a pointer to the starting byte :) But how do I read, say, first 4 bytes and turn them into an integer value? And then next 8 bytes and turn them into double float? In Delphi, I use move procedure for this. You just give it a pointer to the source, a pointer to the destination and a number of bytes to read - works like a charm.
So, 1) how to read bytes and write them into variables? 2) how to perform pointer arithmetics in Fortran (for example, to move pointer X bytes forward in address space)?

Here is the listing of my subroutine (it's a dll, btw):

Code:
function FSReadGridData
 ! Func returns error code, 0 for success
 
 !DEC$ ATTRIBUTES DLLEXPORT::FSReadGridData

 USE sizeconstants 
 USE Kernel32, ONLY: OpenFileMapping, MapViewOfFile   

 ! Variables

  Implicit NONE
 !---------------------------------
  Integer(4)      :: FSReadGridData 
 !---------------------------------
   
  Character(LEN=63)           :: MapName 
  Byte, Pointer                   :: GM_start, GM_caret
  Integer(SHANDLE)            :: GeometryMH   ! SHANDLE =4 for Win32
  
  Integer(4), Dimension(1:10) ::  test1 ! arrays to store test data from memory file
  Real(8), Dimension(1:10) ::  test2 


 !=============================

  MapName = 'PKSigma_Grid_Map'//CHAR(0) ! We need a null-terminated string

  GeometryMH = OpenFileMapping(FILE_MAP_READ,.FALSE.,MapName)

  if (GeometryMH.EQ.0) then
    FSReadGridData =2001
    return
    ! return error code if could not open mapped file
  end if

  GM_start = MapViewOfFile(GeometryMH,FILE_MAP_READ,0,0,0);
  GM_caret = GM_start;
  
    ! GM_start holds pointer to starting byte, we 'll need it later to unmap view
    ! Using GM_caret for current location 

   ! ...NOW I need to read 40 bytes from GM_caret and write them to test1 array, how do I do that?!

end function FSReadGridData
 
Technology news on Phys.org
  • #2
I think I got it. I just have to use WinApi function CopyMemory and fortran loc function in pointer arithmetics.
 
  • #3



I understand the frustration of trying to learn a new programming language, especially when it comes to something as specific as reading from a memory-mapped file. However, I am confident that with some guidance, you will be able to accomplish your task in Fortran.

Firstly, Fortran has built-in functions for reading and writing binary data, such as the READ and WRITE statements. These allow you to specify the number of bytes to read or write, making it similar to the "move" procedure you mentioned in Delphi. For example, to read 4 bytes into an integer variable, you could use the statement:

READ (GM_caret, '(I4)') test1(1)

This will read the first 4 bytes at the current location of GM_caret and store it in the first element of the test1 array. You can also use the same format for reading double precision floating-point numbers (REAL*8) by changing the "I4" to "F8".

Secondly, to perform pointer arithmetic in Fortran, you can use the INQUIRE statement to get the current position of the pointer and then use the SEEK statement to move it to a specific location. For example, to move the pointer 8 bytes forward, you could use the statements:

INQUIRE (GM_caret, POS=curpos)
SEEK (GM_caret, POS=curpos+8)

This will move the pointer to 8 bytes ahead of its current location.

I hope this helps you with your Fortran code and good luck with your research!
 

1. How do you open a memory-mapped file in Fortran?

To open a memory-mapped file in Fortran, you can use the OPEN statement with the ACCESS='MAPPED' option. This will allow you to access the file's contents directly from memory.

2. How do you read data from a memory-mapped file in Fortran?

To read data from a memory-mapped file in Fortran, you can use the READ statement with the UNIT and POS options. The UNIT option specifies which file to read from, and the POS option specifies the starting memory address to read from.

3. Can you read and write to a memory-mapped file in Fortran?

Yes, you can read and write to a memory-mapped file in Fortran. To write to a memory-mapped file, you can use the WRITE statement with the UNIT and POS options. Just make sure to use the correct data type and format when writing to the file.

4. How do you close a memory-mapped file in Fortran?

To close a memory-mapped file in Fortran, you can use the CLOSE statement with the UNIT option. This will release any resources associated with the file and close the file for further access.

5. Are there any limitations to using memory-mapped files in Fortran?

Yes, there are some limitations to using memory-mapped files in Fortran. One limitation is that the file size cannot exceed the available memory on your system. Additionally, memory-mapped files can only be accessed in fixed-size increments, so it may not be suitable for all types of data. It's important to carefully consider your data and system limitations before using memory-mapped files in Fortran.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top