473,804 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3067
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*******@gmai l.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.program mer.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*******@gmai l.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(hI nstance,"name_o f_your_resource ","your_custom_ resource_type") ;
DWORD size = SizeofResource( hInstance,hres) ; // size of your file
HGLOBAL hglob = LoadResource(hI nstance,hres);
void* p = LockResource(hg lob); // 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
2232
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
2
2382
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 derivatives of that. However, the build-in exceptions cover most of the error types that occur in a standard program. For example, my module communicates with measurement instruments, so any communication error would fit perfectly to the build-in...
3
4798
by: fragget | last post by:
where can i find a plugin that will automatically update the build number in vs.net? thx fg
12
6121
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 out of which unmanaged exception //get thrown
4
1254
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 own web site (i.e. IP, root folder, etc.). Each accesses it's own SQL Server 2000 database. Let's call them App A and App B. If I open a page in App A that starts a long-running process...so it's sitting there in perpetual "loading page" state...
5
1566
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 declared These errors are totally erroneous. If I go into the class and cut the offending code and repaste it in the same spot the errors temporarily go away. Also, more effective is to delete the files in project/bin and rebuild. Both fixes...
9
1986
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 side-by-side assemblies and that application has to have a manifest binding it to those assemblies. My question is, can previously built apps use my new dlls build with vs2005? It would be really important to make it work, because otherwise we would...
2
1841
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" configuration). What is the consequence of doing it this way? The website works etc but I read that the performance of the website can be impacted because building it in debug configuration causes the website to not cache scripts or something like...
3
3123
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 can't find it. i'm used to Visual Basic 2005, so some help would be appriciated. i need to find the build events tag because i'm creating a screen saver. perhaps there was an error with my installation. PLEASE HELP!!!
1
1399
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 command limit has been reached. For more information on this error, please refer to Microsoft knowledge base article 810886. Hosting on a UNC share is not supported for the Windows XP Platform. / Please help me as soon as possible
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.