473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to build read-only file into exe file?

Hi all,

A read-only data file is read in a C/C++ program.
And now I use stdio function such as fopen() to fread() to operate the
file.
The content of the data file is constant.

How to build the file into exe file.

Any suggestions will be appreciated!
Best regards,
Davy

Nov 10 '05 #1
7 3038
Davy wrote:
Hi all,

A read-only data file is read in a C/C++ program.
And now I use stdio function such as fopen() to fread() to operate the
file.
The content of the data file is constant.

How to build the file into exe file.


You can't do it in portable C.

What you /can/ do is to turn the data file into the value of an
array and work from that.

It's a shame you can't (portably) use the file-stream reading
operations to read such an array. (In like circumstances, I
invented my own pseudostream-reading operations, and made
implementations that read from FILE* streams and others that
read from strings/arrays.)

--
Chris "one-track" Dollin
Capability does not imply necessity.
Nov 10 '05 #2
Davy wrote:
Hi all,

A read-only data file is read in a C/C++ program.
And now I use stdio function such as fopen() to fread() to operate the
file.
The content of the data file is constant.

How to build the file into exe file.

Any suggestions will be appreciated!
Best regards,
Davy


Convert the file to a list of characters (or ints or whatever) in an
array, e.g.,

unsigned char data[] = { 42, 50, 33, /*...*/ };

Then, use that array in your program.

Cheers! --M

Nov 10 '05 #3

"Davy" <zh*******@gmail.com> wrote
A read-only data file is read in a C/C++ program.
And now I use stdio function such as fopen() to fread() to operate the
file.
The content of the data file is constant.

How to build the file into exe file.

The term "exe" is a hint that you are compiling under MS Windows.
The Microsoft Visual C++ compiler does provide facilities for including
"resources" in your executable. The only C way of converting these into a
file is to create a temporary woth tmpfile(), fwrite in the data, rewind the
file, and then pass to the read functions. Horribly inefficient, but that
matters much less these days than it used to.

Nov 10 '05 #4
Hi,

Thank you for your help :-)

Yes, I use VC compiler. And can you provide some hint on using
"resources" in executable?

Any suggestions will be appreciated!
Best regards,
Davy

Nov 22 '05 #5
Davy wrote:
Hi,

Thank you for your help :-)

Yes, I use VC compiler. And can you provide some hint on using
"resources" in executable?

Any suggestions will be appreciated!


The best suggestion we can give in comp.lang.c (and probably
comp.lang.c++) is to discus your system in a system specific news group.
I've set follow-ups to be only comp.os.ms-windows.programmer.win32 since
that is the most likely of the groups you have posted this to.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 22 '05 #6

"Davy" <zh*******@gmail.com> wrote
Thank you for your help :-)

Yes, I use VC compiler. And can you provide some hint on using
"resources" in executable?

Not really.
The situation is that the ANSI C standard, which this newsgroup discusses,
doesn't provide any really good way of incorporating non-code data into
programs.
Small items like strings or tables can be embedded into C source, but one
you have more than a hundred or so values this approach rapidly breaks down.
The ANSI way of solving the problem is to write a little program that read
in binary data (images, sound, 3d graphics coordinates etc) and outputs C
arrays as text.
This isn't particularly convenient, so Microsoft have helpfully provided a
"resource compiler" that effectively does this for you.
There's nothing wrong with using the Microsoft tool, but it means that your
program is no longer, strictly, a C program. Thus it becomes off-topic on
comp.ang.c
Nov 22 '05 #7
Davy wrote:
Hi,

Thank you for your help :-)

Yes, I use VC compiler. And can you provide some hint on using
"resources" in executable?

Any suggestions will be appreciated!
Best regards,
Davy

Simply add resource script file to the project and then import your file as a custom resource type.
Using custom resource:

HINSTANCE hInstance = GetModuleHandle(NULL);
HRSRC hres = FindResource(hInstance,"name_of_your_resource","yo ur_custom_resource_type");
DWORD size = SizeofResource(hInstance,hres); // size of your file
HGLOBAL hglob = LoadResource(hInstance,hres);
void* p = LockResource(hglob); // now p points to your file in memory
Nov 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
2
by: Torsten Bronger | last post by:
Hallöchen! I write a module, and so I must raise exceptions at several points. The PEP 8 says that a module should define their own exceptions base class, derived from "Exception", and...
3
by: fragget | last post by:
where can i find a plugin that will automatically update the build number in vs.net? thx fg
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
4
by: Dave Diehl | last post by:
Has anyone heard of a problem with a rebuild of one application causing a problem with another app on the same server? I have two asp.net applications on the same Win2000 server, each with it's...
5
by: Jon | last post by:
I am constantly getting reports in my task list like these: * 'someObjectName" is not a member of 'someOtherObjectName' * Handles clause requires a WithEvents variable. * Name 'someName' is not...
9
by: yevvi | last post by:
Hi, We have a product with bunch of dlls which are now built with Visual Studio 2003. We want to switch the build to use VS2005. I have read that in VS2005 runtime libraries come as...
2
by: NH | last post by:
I am a bit confused with the difference between building and publishing a website using asp.net 2.0. Currently I build the website up to the server (in the config manager this is in "Debug"...
3
by: =?Utf-8?B?Rmxhc2hwcm8=?= | last post by:
i have googled this question but cannot find an answer. i'm running windows vista and i'm using Visual Basic Express 2008. i know the build event button SHOULD be in under the compile tag but i...
1
by: =?Utf-8?B?QnJ5YW4gQW50aG9ueQ==?= | last post by:
Hi to all, i encounter a problem with this: Failed to start monitoring changes to '\\MLAIS001C\Bryan.Bequiso$\Cached\My Documents\Visual Studio 2005\Projects\Sand_Box' because the network BIOS...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.