By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

How to build read-only file into exe file?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.