Hi,

I'm trying to write a program to read in a file (over a 100 records, each being a union of 3 structures), sort them, print them out and write the sorted data to the another file.

It does read them in and sort them and this seems to work, but cant seem to access the block of memory to print them out.

I have a pointer to the union transaction records *s and s is the start of the block of memory for the file read.

I declared a pointer to s after the sort and then tried to print out eg transaction.ir.type etc and then increment the pointer but I don't get anything output.

help!!

Andy.

Here's the code (I know its a bit messy)

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>


#define LINES_PER_PAGE 60

void quantity(void);
void error(int i);
void valid(void);
void valid_part_number(void);
char Pause(void);
void read(void);
void valid_cust_name(void);
void valid_address(void);
void cust_balance(void);
void cust_credit(void);




FILE *fp_ptr,
*prn_ptr,
*sorted_ptr,
*valid_ptr;

struct ir_rec { /* Insert/replace record */
char type, /* record type */
cust_code[6], /* customer code */
part_no[7], /* part number */
quantity[21]; /* issue/ received quantity */
};

struct del_rec { /* deletion record */
char type,
cust_code[6];
};

struct create_rec { /* creation record */
char type,
cust_code[6],
cust_name[21], /* cust name */
cust_address[61], /* address */
cust_balance[10], /* balance */
cust_credit_limit[8]; /* credit limit */
}dummy;


union records {
struct ir_rec ir;
struct del_rec del;
struct create_rec create;
};

union records transaction; /* transaction is union name */

int intcmp(const void *v1, const void *v2);



char big[150],
*trans_ptr,
record[5];

int *big_ptr,
delta = 0,
page = 0,
lines = 0;


void main()
{
/*CHANGED THIS BIT FROM A CHARACTER POINTER TO A UNION POINTER*/

/*CHANGED IT AGAIN IT SHOULD BE A POINTER OF UNION RECORDS TYPE*/
union records *s;

int size_of_file,
i,
r,
d,
c,
diff_cust,
loop;

char *ptr,
prev_cust[6];



/* struct create_rec dummy = {'\0',
"\0",
"\0",
"\0",
"\0",
"\0" }; */

i=r=d=c=0;

/* open file for output */

if(( sorted_ptr = fopen("A:\SORTED.dat","wb")) == NULL)

{
printf("\nError creating output file\n");
exit(1);
/* unable to create/write to output file */
}
/*OPEN FILE FOR INPUT */

if(( valid_ptr = fopen("A:\ZENCVF.DAT","rb")) == NULL)

{
printf("\nError creating output file\n");
exit(1);
/* unable to create/write to output file */
}

/* calculate size of file */
/* find end of input file */

if(fseek(valid_ptr, sizeof(transaction), SEEK_END)!=0 )
{
printf("\nSeek Error.");
exit(1);
}


size_of_file = (ftell(valid_ptr) / sizeof(transaction));

s=(union records *) malloc (size_of_file);
rewind(valid_ptr);


/* write union to memory */
/* while(!feof(valid_ptr)) */




/*REMOVED AMPERSAND FROM IN FRONT OF S*/
fread(s, sizeof(transaction)* size_of_file, 1, valid_ptr);


Pause();


/* end of file */
printf("\n finished reading in file ");
Pause();
printf("\n %s s",s); /* doesn't work displays nothing*/

/* printf("\n did rewind too %s", s);
Pause();*/

/* read all data into s now time to sort */

/* sort into ascending order */

printf("\n rec number %d", size_of_file);
/* size_of_file is really number of records */

qsort(s, size_of_file, sizeof(transaction), intcmp);
printf("\n finished sort %d", sizeof(transaction));
printf("\n s after sort %s",s);
Pause();
ptr = &s;







/*THE POINTER IS NOT POINTING TO A FILE BUT TO A DYNAMIC ARRAY,POINTER NOT BEING INCREMENTED TO NEXT RECORD ANYWAY.
PERHAPS NEED TO LOOP THRU' THE ARRAY*/
printf("\n I'm in the stats function");


Pause();

for( loop=0; loop<= size_of_file; loop++)

{


printf("\n ptr %p", ptr); /* don't work */
/* prints out <null>???*/

switch(*ptr)
{
case 'I': i=i+1;
break;
case 'R': r=r+1;
break;
case 'D': d=d+1;
break;
case 'C': c=c+1;
break;
}
printf("\n i%d %d %d %d", i,r,d,c);
printf("\n 1%c 2%s ", transaction.ir.type, transaction.ir.cust_code);
Pause();
(*ptr)++ ;


}
if( strcmp( transaction.ir.cust_code, prev_cust) != 0)
{
diff_cust +=1;
}

strcpy( prev_cust, transaction.ir.cust_code);

Pause();
/* end of file */


Pause();
/*rewind(valid_ptr);*/



printf("\ got to before while for fwrite");



fwrite(s, sizeof(transaction)* size_of_file,1, sorted_ptr);





printf("\n wrote to file");


fclose(sorted_ptr);
fclose(valid_ptr);
fclose(fp_ptr);
fclose(prn_ptr);

}

int intcmp(const void *v1, const void *v2)
/* the sort bit */
{



/*return ((*(char *)v1) - (*(char *)v2));*/
/*CHANGED THIS BIT TO USE THE UNION TYPE NAME (records) INSTEAD OF THE DEFINED NAME (transaction)*/
return (strcmp(((union records *)v1)->ir.cust_code,((union records *)v2)->ir.cust_code));

}



char Pause(void)
{
char c;
printf("\nPress Enter to continue.");
while((c=getchar()) != '\n')
{
return c;
}
return(1);
}