Suppose I have five binary files on disk. These five files encode sequences of numbers in a binary format except they all have different types. Let’s say they are SHORT INT, INT, LONG, FLOAT, and DOUBLE. We’ll let the files also have a different number of numbers stored in them.
Now suppose I have a program that when executed, asks the user
Enter filename you wish to load:
and the user can pick one of these five files. I would like the program to load these files into an array so that further calculations may be performed.
We may assume there exist two functions: one that returns an integer value that encodes the type (i.e., SHORT INT, INT, LONG, FLOAT, or DOUBLE) of the file (call it “getfiletype”) and one that returns the number of numbers (e.g., 1000, 9338, 8131, 0, etc.) in the file (call it “getfilesize”). The actual number of entries may be billions of numbers large. If possible, within the program I would like to use the same name for the array (say “array”) that holds the values of whatever file the user picked. That way I can have blocks like
N=getfilesize("pickedfile.dat");
for(i=0 ; i<N ; i++ ) {
doublearray[i]==2.0*(double)array[i]+7.12;
}
that can transform the array. Here I have introduced a new array called “doublearray” to hold the transformed values. This double array would then either be written to a file in format DOUBLE or converted before write to one of the other formats.
How do I do this sort of thing? Throughly confused.
Your program could create a static (or global) void* array that would be populated by the latest call to either getfiletype() or getfilesize(). You would then have to appropriately cast it (as you’ve done specifically to double in your example). It is assumed you’ve solved the loading of the file data aspect.
Although that approach is not really very clean in terms of usage (relying on a static or global would limit your options for performing different conversations – specifically as it currently stands you would have to ensure you operate in a sequential manner).
A cleaner API would perhaps be something along these lines:
You can then use it like:
IMHO C++ would make this even nicer (as you could use templates), but as this question is only tagged with C I’m just presenting options in C.