How to build read-only file into exe file?

D

Davy

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
 
C

Chris Dollin

Davy said:
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.)
 
M

mlimber

Davy said:
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
 
M

Malcolm

Davy said:
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.
 
D

Davy

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
 
F

Flash Gordon

Davy said:
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.
 
M

Malcolm

Davy said:
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
 
D

/dev/null

Davy said:
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","your_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top