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

how to incorporate data file into fortran executable?

Gennady_Stupakov
Beginner
1,943 Views
Hello,

I have a fortran program which reads some numerical data from an external file during execution. The file is rather large and has many lines of numbers. The file should be located in the same directory where the executable is. I am wondering if there is a way to include the data file into the executable one during compilation, and make the program read this data from itself. I understand that this would make my executable larger, but this is not an issue. This would eliminate the need to keep the file in the right directory during execution.

Thank you.
GS
0 Kudos
4 Replies
IanH
Honored Contributor III
1,943 Views
A windows platform specific solution is to put data like that in the executable's resources. Have a look at the topic "User-Defined resource" (in the context of the scripts for the Windows resource compiler tool) on msdn or your local help for a starting point.

The only examples I have to hand are C++ ones, but it shouldn't be too hard to make it work with the support provided by the IFWIN modules etc. Key API's are FindResource, LoadResource, and LockResource.

If your external file is text, then I'd be inclined to "compile" it into a suitable in-memory (binary) representation before binding it as a resource, so that you don't have to muck around with lots of character variable manipulation and internal reads in order to access the data. You might be able to extract your current file reading procedures from your program to use as the basis for a little "compiler" program that is built and then called as a part of your main program's build process. Once you've got your in-memory representation then use STREAM io to dump that memory to a file and that is then your user defined resource data. Be mindful that the in-memory representations can change with things like compiler flags due to padding and that dumping the in-memory representation of things like array descriptors, pointers and non-sequence derived types won't work - this is really only suitable if your data can be represented easily in memory as a couple of boring old-style fortran arrays.

Alternatively, bind the external file as-is, and then when the program is run, make it check its working directory for a different pre-existing, suitable external file and if it can't find one, then have it regurgitate the resource into the working directory as a default input file. Then your program can read the external file exactly as it does now and the users also get the opportunity to customise the settings.

IanH
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,943 Views
See this thread. For your convenience, the program which demonstrates how to attach and extract an arbitrary file is attached below. Its "attachment" is its own zipped source, thus

  1. Unzip the .exe below
  2. Run the .exe
  3. As result, you get the "REWorkspace.zip", which contains its own source files, that you can now open in Visual Studio.
Note that, strictly speaking, you do not even have to dump the file contents to disk. You can parse them in-memory, because lpVoidData points to its contents, and sVoidData is a byte array containing its contents. For example, you can make sVoidData a CHARACTER(100000) variable, and then READ from it. But it's up to you how you will use it.
0 Kudos
Gennady_Stupakov
Beginner
1,943 Views
Thanks IanH and Jugoslav for answering my question. I will see if I can implement something along the lines of your advices.
Gennady
0 Kudos
GVautier
New Contributor III
1,943 Views
Hello

If you include your data as resource, an advised user can extract those data from the executable.

Another way to embed your data in the executable is :
1st convert your data file in binary file (if necessary)
2nd convert it to an object file with a bin2obj tool
3nd link the object file to your executable

Your data will be accessible from your program as a common array.

0 Kudos
Reply