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

  • Thread starter Thread starter Franzy
  • Start date Start date
  • Tags Tags
    File Fortran
AI Thread Summary
The discussion centers around reading binary data from a memory-mapped file in Fortran, specifically how to convert bytes into integer and double float values. The user has successfully opened the file and created a view but is uncertain about the next steps. They seek guidance on two main points: first, how to read bytes from the memory location and store them in variables, and second, how to perform pointer arithmetic in Fortran to navigate through the data. The user mentions that in Delphi, they use a move procedure for this purpose. They also provide a subroutine listing and express that they plan to use the WinAPI function CopyMemory along with the Fortran loc function for pointer arithmetic to achieve their goal.
Franzy
Messages
2
Reaction score
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
I think I got it. I just have to use WinApi function CopyMemory and fortran loc function in pointer arithmetics.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
16
Views
3K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
1
Views
3K
Replies
2
Views
2K
Back
Top