I am currently using an older sorting code for data and I am trying to modify it to read different data types. The code is actually a sequence of several executables, built from several .c files that then reference some 20+ .h files. I have spent several days on this and have finally found the section of code that opens the data binary data files and reads them. However, there is header information inside the data files, and the code was written for one particular lab's data structure, so a lot of the information regarding skipping header file data is specific to that lab's data structure. As such I am trying to understand how the program skips the header information, so that I can tailor it to my lab's format.
I have run into an issue however, with one particular variable, because I don't fully understand what's going on.
At the top of one of the .h files, among other variable declarations, appears the following:
1 2 3 4 5 6
|
typedef struct
{
unsigned short zed[8192]
}
zeddd;
zeddd zedd;
|
From what I understand, this is creating a new type called zeddd, and that zedd is of type zeddd. Furthermore, the entry for zedd is an unsighed short named zed, which is an array of length 8192.
Now, know this wording is confusing, but this code works as it is so I don't want to change how anything is declared, I'm simply trying to understand it.
Now, for the part I'm stuck at.
Also at the top of the frame is the following:
1 2
|
int zRt = 0; zi =0
u_short *zbuf
|
Then much lower in the code appears the following:
1 2 3 4 5 6 7 8 9 10
|
zRt = read(zinf, &zedd,sizeof(zedd));
if(zRt < 16384)
{
break;
}
for(zi=0;zi<8192;zi++)
{
zbuf[zi] = zedd.zed[zi];
}
|
Okay, so, zinf is a Boolean passed to this .h file from another executable, and in this case the value is 1. The value comes from the following:
zinf = open(name, O_RDONLY, 0)
where name is the full path of a datafile. The program only reaches the other .h file if zinf = 1, zinf = 0 causes the loop to break and it outputs an error.
My issue comes in understanding the bit of code regarding read(zinf, &zedd,sizeof(zedd)) and the zedd.zed[zi] entry.
For the first bit, I thought read needed a file designation. The file opened by zinf = open(name, O_RDONLY, 0) is still open, but why does the "fildes" that read() requires work if its equal to 1? also, what exactly is the &zedd part doing?
And secondly, what is zedd.zed[zi] doing? I thought zed was an array of length 8192, is this re-declaring zed to be an array of size zi? and if so, what is it doing to the variable zedd?
Any help with this would be appreciated, and I'm sorry for the confusing code, it's 2nd hand and I don't have access to the originals nor whom I received it from.