tags:

views:

62

answers:

5

I have a huge data in excel sheet. I need to prepare a structure for that data and fill the data. I can do it in 2 ways.

  1. statically fill the structure during struct initialization struct x a[] = { }

  2. Dynamically fill the structure by allocating memory and filling it in a function.

My structure also looks little complicated. How do I fill this structure?

One record of excel sheet looks like:

region name, list of languages supported, list of encodings supported

How do I define a structure for such data and fill it?

A: 

You won't be able to do 1 if the data is at all dynamic. Putting it in the initialization of the struct in code means just copying all the data into your source file from the excel sheet. I'm guessing this isn't feasible since you said it was huge. You'll have to go with option 2. Unless of course you have the option of using a database library that could read the spreadsheet, that might be your best bet.

bshields
Thanks shields, huge means data is coming to 60 rows.. Still do you thing filling structure dynamically is a better option. Also, this list may get updated in future. Any disadvantages of filling it statically in struct initialization?
Teja
@Teja Yes dynamic is better if the list may ever be updated.
bshields
A: 

If every row contains only 3 columns save it as a CSV file Have a structure with 3 variables all as char* declare 3 char arrays . Then open the file Read every line using fgets Use sscanf on the read string on the arrays. Using the size of the read values allocate memory and store the same in the structure.

Raghuram
A: 

If the data will not change at runtime, you should absolutely use static initialization and declare the object const. On any modern platform, this will result in the operating system sharing the pages with the copy of the executable file in the filesystem cache, meaning it effectively does not use physical memory (if memory is low, the page can be discarded and re-read from the executable file if necessary; it does not need to be kept/swapped to disk).

R..
Thank you! I was little concerned about memory. Now I am clear.I am trying to figure out a struct for it. The requirement is like...One region will have multiple languages, each language can have one or multiple multiple encodings. each region can have one or multiple font files.
Teja
A: 

You didn't say much about the platform that you are developing for. If your compiler is C99 compliant you could initialize your struct itself with named initializers as follows

char *fullname0 = ...
char *phone = ...
.
struct x a[] = {
 [0] = { .fname = fullname0, .phn = phone }
 [1] = { .fname = fullname1, .phn = phone }
};

If it is very much the same when you do this, the best would probably to write a macro for the initialization of each individual struct.

#define X_INITIALIZER(NAME, PHONE) { .fname = NAME, .phn = PHONE }
Jens Gustedt
Note that if you do this, converting the data from CSV to the required format becomes as simple as `sed 's/^/X_INITIALIZER(/;s/$/)/'`. The preprocessor's "stringify" operator `#` may come in handy for this.
R..
A: 

According to your comments, you're only working with sixty rows. That's not huge! Get typing! :-)

But let's work smarter and apply the DRY rule-of-thumb a bit so that the computer does the boring stuff and looks after keeping things up to date as the data evolves. What I suggest you do is to write a separate program to extract the data from the excel sheet and generate a C header file from it so you can just #include the definition. You will then be able to rebuild the structure definition from source with a makefile rule. (Depending on the rules where you work, you might be able to create the helper program in something other than C as it is just a developer tool.)

A final note; it's much simpler to generate static data as arrays of structures (which may contain arrays of structures, etc.) than it is to create linked lists.

Donal Fellows
Yea, I liked your idea. But may take some time :-(. But I will explore how extract from excel sheet and generate a header file.
Teja
Thank you all for the help!!
Teja