- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Unzip the .exe below
- Run the .exe
- As result, you get the "REWorkspace.zip", which contains its own source files, that you can now open in Visual Studio.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks IanH and Jugoslav for answering my question. I will see if I can implement something along the lines of your advices.
Gennady
Gennady
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

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