473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Style - imbedding data files

Greetings,

What is the general practice, usual and customary way,
of including a data file into a source file?

I have some large data structures defined as source
similar to:

=============== ==========
typedef struct fubar
{
unsigned int fab; /* something */
unsigned int nrk; /* somethang */
} Fubar;

static Fubar myFubar[] =
{
{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21}
};
=============== ==========

The above data array is used by a single
source file. Currently, it is hard-coded
into the source file, but I need to move
it into a separate file and include it.
(The situation is actually much more
complicated with multiple large data
arrays that are all used by the same
single source file.)

Is it alright to put that data structure
definition into a ".h" header file, even
though it is only used by a single source
file? Is there some other "usual and customary"
style for imbedding the source of a data
definition other than using a header file?

The data arrays are only defined in source
form, not binary files or such like.

TIA

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #1
18 1571
xarax wrote:

What is the general practice, usual and customary way,
of including a data file into a source file?

I have some large data structures defined as source
similar to:
I suggest the following:

---- FILE fubartype.h ----
#ifndef fubartype_h
#define fubartype_h typedef struct fubar
{
unsigned int fab; /* something */
unsigned int nrk; /* somethang */
} Fubar; #endif
----- EOF fubartype.h -----
----- FILE fubar.c ----
#include "fubartype. h"
#include "fubar.h" static Fubar myFubar[] =
{
{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21}
};


/* some code that provides access to myFubar */
/* and prototyped in fubar.h for use elsewhere */
----- EOF fubar.c -----

and a further file "fubar.h".

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #2
xarax wrote:
Greetings,

What is the general practice, usual and customary way,
of including a data file into a source file?

[snip]

I always like a third alternative.
I prefer to place data into an assembly language file.
Many assembly languages offer better control over the
location and attributes of the data.

The assembler I'm working with has a directive to include
a binary file. The language allows me to align the data
and place it into a read-only segment. The tricky part
is figuring out how to declare the location in C and
access it.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3
In <wb************ **@newsread2.ne ws.pas.earthlin k.net> "xarax" <xa***@email.co m> writes:
What is the general practice, usual and customary way,
of including a data file into a source file?

I have some large data structures defined as source
similar to:

============== ===========
typedef struct fubar
{
unsigned int fab; /* something */
unsigned int nrk; /* somethang */
} Fubar;

static Fubar myFubar[] =
{
{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21}
};
============== ===========

The above data array is used by a single
source file. Currently, it is hard-coded
into the source file, but I need to move
it into a separate file and include it.
(The situation is actually much more
complicated with multiple large data
arrays that are all used by the same
single source file.)

Is it alright to put that data structure
definition into a ".h" header file, even
though it is only used by a single source
file?
Nope. It's not a header file, therefore you don't use the .h suffix.
Is there some other "usual and customary"
style for imbedding the source of a data
definition other than using a header file?
Yes, you include a .c file, containing the initialiser for your array.
The data arrays are only defined in source
form, not binary files or such like.


That's why you use a .c file for the purpose.

Header files have a well defined purpose and it isn't providing
initialisers for your arrays. OTOH, it's perfectly OK to include a .c
file from another .c file. It doesn't harm to put a comment at the
beginning of the included .c file, explaining that it is supposed to be
included by another file, rather than being compiled as such.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
"xarax" <xa***@email.co m> wrote in
news:wb******** ******@newsread 2.news.pas.eart hlink.net:
Greetings,

What is the general practice, usual and customary way,
of including a data file into a source file?

I have some large data structures defined as source
similar to:


I'd do it with a data C file, function C file, an external consumer header
file and a "private" header file. E.g.

foo.h - External consumer include this file to use the foo API
foo.c - Contains the foo API functions and constants
foo_data.c - Contains large data tables with external linkage
foo_data.h - Contains externs for large data tables but not
to be included by external consumers, just foo.c.

--
- Mark ->
--
Nov 14 '05 #5
Dan Pop wrote:

In <wb************ **@newsread2.ne ws.pas.earthlin k.net> "xarax" <xa***@email.co m> writes:
What is the general practice, usual and customary way,
of including a data file into a source file?

I have some large data structures defined as source
similar to:

============== ===========
typedef struct fubar
{
unsigned int fab; /* something */
unsigned int nrk; /* somethang */
} Fubar;

static Fubar myFubar[] =
{
{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21}
};
============== ===========

The above data array is used by a single
source file. Currently, it is hard-coded
into the source file, but I need to move
it into a separate file and include it.
(The situation is actually much more
complicated with multiple large data
arrays that are all used by the same
single source file.)

Is it alright to put that data structure
definition into a ".h" header file, even
though it is only used by a single source
file?
Nope. It's not a header file, therefore you don't use the .h suffix.


Chapter and verse, please.
Is there some other "usual and customary"
style for imbedding the source of a data
definition other than using a header file?


Yes, you include a .c file, containing the initialiser for your array.
The data arrays are only defined in source
form, not binary files or such like.


That's why you use a .c file for the purpose.

Header files have a well defined purpose and it isn't providing
initialisers for your arrays.


Chapter and verse, please.
OTOH, it's perfectly OK to include a .c
file from another .c file. It doesn't harm to put a comment at the
beginning of the included .c file, explaining that it is supposed to be
included by another file, rather than being compiled as such.


The choice of what to call the #include'd files should
be driven by the tools and practices in use where the code
is written and maintained, because the C language itself
provides practically no guidance. One practice that's been
found useful in some circumstances:

- If the big blob of data is "source" in the sense that
it's written once and then left alone (barring errors
and ordinary hand-edited upgrades), name the file in
the same manner as other source files. This will most
likely result in a .c or .h name, and although Dan is
vehement in his opinion about which to use, I really
think it's up to you.

- If the big blob is not "source" but is the output of
a helper program of some kind, use a name that suggests
the non-source nature: .cdata or .gen or some such.
This will help your code management and other tools
distinguish "sources" from "products" more easily, and
will help your programmers do the same.

Names should be your servants, not your masters.

--
Er*********@sun .com
Nov 14 '05 #6
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:

In <wb************ **@newsread2.ne ws.pas.earthlin k.net> "xarax" <xa***@email.co m> writes:
>What is the general practice, usual and customary way,
>of including a data file into a source file?
>
>I have some large data structures defined as source
>similar to:
>
>============== ===========
>typedef struct fubar
>{
> unsigned int fab; /* something */
> unsigned int nrk; /* somethang */
>} Fubar;
>
>static Fubar myFubar[] =
>{
> {0x01,0x02},
> {0x02,0x03},
> /* much more of the same */
> {0x20,0x21}
>};
>============== ===========
>
>The above data array is used by a single
>source file. Currently, it is hard-coded
>into the source file, but I need to move
>it into a separate file and include it.
>(The situation is actually much more
>complicated with multiple large data
>arrays that are all used by the same
>single source file.)
>
>Is it alright to put that data structure
>definition into a ".h" header file, even
>though it is only used by a single source
>file?


Nope. It's not a header file, therefore you don't use the .h suffix.


Chapter and verse, please.


Idiotic request.
>Is there some other "usual and customary"
>style for imbedding the source of a data
>definition other than using a header file?


Yes, you include a .c file, containing the initialiser for your array.
>The data arrays are only defined in source
>form, not binary files or such like.


That's why you use a .c file for the purpose.

Header files have a well defined purpose and it isn't providing
initialisers for your arrays.


Chapter and verse, please.


Idiotic request.
OTOH, it's perfectly OK to include a .c
file from another .c file. It doesn't harm to put a comment at the
beginning of the included .c file, explaining that it is supposed to be
included by another file, rather than being compiled as such.


The choice of what to call the #include'd files should
be driven by the tools and practices in use where the code
is written and maintained, because the C language itself
provides practically no guidance.


The word "header" provide plenty guidance, for those with a clue. And the
..h suffix implies header file.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

On Thu, 19 Feb 2004, xarax wrote:

What is the general practice, usual and customary way,
of including a data file into a source file?


#include. ;-)

You've gotten several decent answers, but I thought I'd throw
in my two cents. Here are a couple ways to do it, depending on
your exact circumstances.

METHOD 1. Canonical multi-source-file approach. Forget about
'static', put the array in its own source file, add an 'extern'
declaration somewhere, and compile everything together.
Pros: Easy to write and ultra-portable. Easy to understand.
Cons: You must make sure 'myFubar' doesn't encroach on the
namespace of anything else in your project, because it's now got
external linkage instead of internal. "main.c" cannot access
'sizeof myFubar' as it could before, because 'myFubar' is an
incompletely typed object.
Note: how I used 'H_FUBAR' instead of 'fubar_h' (to avoid
accidentally hitting reserved namespaces). The 'typedef' style
I prefer (which in this case is religious, but makes sense for
more complicated or (mutually) recursive types). The 'extern'
declaration in "main.c".

==begin fubar.h==

#ifndef H_FUBAR
#define H_FUBAR

typedef struct fubar Fubar;

struct fubar
{
unsigned int fab; /* something */
unsigned int nrk; /* somethang */
};

#endif

==end fubar.h==
==begin fubar-data.c==

#include "fubar.h"

Fubar myFubar[] =
{
{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21}
};

==end fubar-data.c==
==begin main.c==

#include "fubar.h"
extern Fubar myFubar[]; /* defined in "fubar-data.c" */

[...use myFubar...]

==end main.c==
METHOD 2. The preprocessor-hack approach. This method can get
a lot more complicated, if you want to process the same data in
several different ways, but here's the really simple method.
Pros: You can extend this method to do crazy things with the
preprocessor, such as creating another array of string representations
of the list, or even more complicated things (for which I unsuccessfully
searched Google Groups, but won't post my own icky examples unless
asked ;-)
Cons: You've got to come up with a good mnemonic extension for the
"fubar-data" file. :)
Note: the extra comma in "fubar-data.dat" this time. C allows the
trailing comma in array initializers precisely because some people
like to feed machine-generated data to C compilers, as you're trying
to do (I presume). Note also that if you understand how the line
#define ELEMENT(x,y) {x,y},
inserted in "main.c" might apply to this example, then you see what
other complicated things the preprocessor can do. :-)
While it's not invalid to call the data file "fubar-data.h" or
"fubar-data.c", I certainly wouldn't, because the former invites the
next guy to write #include "fubar-data.h" at the top of his program
(which won't work), and the latter invites him to invoke
"cc fubar-data.c", (which won't work). Better to use an extension
without those connotations (or no extension at all!).

==fubar.h same as above==
==begin fubar-data.dat==

{0x01,0x02},
{0x02,0x03},
/* much more of the same */
{0x20,0x21},

==end fubar-data.c==
==begin main.c==

#include "fubar.h"

static Fubar myFubar[] = {
#include "fubar-data.dat"
};

[...use myFubar...]

==end main.c==
HTH,
-Arthur

Nov 14 '05 #8
I thought
.c files are the target of cc
.h files are the target of #include
What's wrong with that? Too simplistic?

Da*****@cern.ch (Dan Pop) wrote in message news:<c1******* ***@sunnews.cer n.ch>...
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:
Nope. It's not a header file, therefore you don't use the .h suffix.

Chapter and verse, please.

Idiotic request.


Hi Dan! Still reading characters one at a time?

I too would like to see a standards quote, if it exists,
which would contradict my understanding.
Does that make me an idiot too?
Yes, you include a .c file, containing the initialiser for your array.


No. .c files are for cc; .h files are #included. See above.

James
Nov 14 '05 #9


James Dow Allen wrote:
I thought
.c files are the target of cc
.h files are the target of #include
What's wrong with that? Too simplistic?

Da*****@cern.ch (Dan Pop) wrote in message news:<c1******* ***@sunnews.cer n.ch>...
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:

Nope. It's not a header file, therefore you don't use the .h suffix.

Chapter and verse, please.


Idiotic request.



Hi Dan! Still reading characters one at a time?

I too would like to see a standards quote, if it exists,
which would contradict my understanding.
Does that make me an idiot too?

Yes, you include a .c file, containing the initialiser for your array.



No. .c files are for cc; .h files are #included. See above.

James


The names (and extensions) of files are a convention and,
to my knowledge, are not mandated by the standard.
Nor is there any prohibition about including any file
name with any extension.
(Please correct me if I am wrong.)

I may, if I desire, #include "main.c" (or a file
which has the code for main() ),
in a code module and compile it and, unless it
has errors, get an executable out of the compilation
process. Whether that is good practice or not is
debatable, but it's allowed.

e.g. (pseudo code)

#define FUNC myfunc

#include "main.c" /* which calls FUNC(args) */

int myfunc(args) {
/* do some stuff */
return value;
}

I then compile "myfunc.c" and hopefully have
a running program. I think this is perfectly
legal by the standard?

--
Ñ
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #10

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

Similar topics

38
3483
by: Ken Davies | last post by:
I'm relatively new to style sheets but I've been unable to find the answer to a - hopefully - simple question. Is it valid to use more than one <LINK REL=STYLESHEET..... in a document. Basically I'm working on a web site with several different sections so I'm hoping to create one external global style sheet and seperate external style sheets...
7
1643
by: Cameron Hatfield | last post by:
I was wondering how good this page of coding standards (http://www.doc.ic.ac.uk/lab/cplus/c++.rules/) is. Is it too outdated?
12
3878
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function...
4
5452
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
3
2117
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop...
9
2016
by: Radium | last post by:
Cascading Style Sheet is an extreme hazard to your privacy. It allows others on the internet to see your monitor and files. It allows them to copy images on your monitor to their computers. It also allows them to copy files from your computer to their's. It is dangerous. Avoid at all costs. CSS that isn't stored in the victim's computer....
18
2731
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and...
3
1840
by: Henrik | last post by:
My web pages of course use css files, but Webmail services require style to be in element style attributes. How do I convert a standard HTML page to HTML suitable for delivery to webmail services? Perhaps another way to ask the question is: How to I walk the DOM and for every element, for every style, set the element's style attribute...
5
10486
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a machine running IIS 6.0. I just replaced the web.config and several aspx pages in the application and now the style sheets are not working. the images from the themes work but not the css files. Any help. -- Jerry
0
7541
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...
0
7734
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. ...
1
7497
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...
0
7826
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6065
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...
0
3512
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...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.