First, you need to typedef your structures correctly, and then put your union up under your typedefs--
Code:
/*****
*
* TYPEDEFS
*
*****/
typedef struct
{
char record_type;
char cust_code[7];
char part_num[7];
int quantity;
}I_R_details;
typedef struct
{
char record_type;
char cust_code[7];
}deletion_details;
typedef struct
{
char record_type;
char cust_code[6];
char cust_name[21];
char cust_addr[61];
float cust_bal;
long credit_limit;
}creation_details;
typedef union customer
{
I_R_details first;
deletion_details second;
creation_details third;
}unval;
Then, take a look at your actual read command-- from what I glanced at in your code, I can't see that you've asked it to read in anything--
Code:
while(fread(&unval, SIZE, 1, unval_ptr) == 1)
]
fread() requires 4 things to be passed to it:
1) The address of a buffer to read data into
'unval' isn't a variable, it is a TYPE of variable. Just as 'char' is not a variable it is a TYPE of variable.
2) The size of the element to be read
'SIZE' isn't defined anywhere that I see. It is supposed to be coded as something like 'element_size = sizeof(unval);'. If 'SIZE' is currently zero, that would explain why you're not getting any input.
3) The Number of Elements to read
4) The file pointer.
Furthermore, if you know how many elements to read, then you don't need to use the while() statement. All you have to do is have a buffer larger enough for all of them and then access them as an array.
You should also zero the file pointers (any pointer for that matter) prior to using them.
Note that you should also use binary mode when reading/writing this data.
For Example:
Code:
/*****
*
* TYPEDEFS
*
*****/
typedef struct
{
char record_type;
char cust_code[7];
char part_num[7];
int quantity;
}I_R_details;
typedef struct
{
char record_type;
char cust_code[7];
}deletion_details;
typedef struct
{
char record_type;
char cust_code[6];
char cust_name[21];
char cust_addr[61];
float cust_bal;
long credit_limit;
}creation_details;
typedef union
{
I_R_details first;
deletion_details second;
creation_details third;
}unval;
. . .
int main(void)
{
size_t bufSize;
FILE *myFileP;
unval *bufferP;
size_t elemSize;
elemSize = sizeof(unval);
myFileP = 0L;
bufSize = (elemSize * 10); /* we have 10 entries to read */
bufferP = 0L;
bufferP = (unval*)malloc(bufSize); /* allocate buffer to hold all 10 */
if(!bufferP)
{
printf(stderr,"\n Error in allocating input buffer");
exit(1);
};
myFileP = fopen("zentd.dat","r+b");
if(!myFileP)
{
printf(stderr, "\n Error in opening file GD03TD ");
exit(1);
};
fread(bufferP,elemSize,10,myFileP);
. . .
/* after you've read it, you can then access it */
}
Since the first 2 fields of every structure are the same, I would re-typedef the structures and union as follows:
Code:
typedef struct
{
char part_num[7];
int quantity;
}I_R_details;
typedef struct
{
char cust_name[21];
char cust_addr[61];
float cust_bal;
long credit_limit;
}creation_details;
typedef struct
{
char record_type;
char cust_code[7];
union
{
I_R_detail irInfo;
creation_details cInfo;
};
}customer;
enjoy.