I am by rights a VB programmer, and am trying to write a c++ routine to sort some data. I need help with trying to figure out:
1) how to use VectorFromBstr, I added the oleauto.h file, but I still get undeclared identifier..
2) how to write my custom compare function for qsort, listed far below, to compare each field of the type Patient..
Before that, a little background:
I have a file of patient data, written by the following VB type:
Code:
Type P1
LName As String * 16 'last name
FName As String * 12 'first name
MName As String * 1 'middle initial
BDate As String * 10 'birthdate
NDate As String * 10 'next office visit
Upi As Integer 'unique patient identifier
End Type
My VB program does not sort the records in the file, but instead loads the file contents into memory, and sort them, in memory. I then run through the sorted array of patient records, writing the UPI's to an index file.
(bare with me here)
As an example if I have three patients, John with UPI of 5343 , Brad with UPI of 9232, Tim with UPI of 2322, the array would be sorted, and index file would contain:
9232
5343
2322
OK, now, what I did above, I want to accomplish in C++, which is basically: open the patient file, read in the file, qsort the patients, and write the sorted UPI's(which are just integers) to file.
So this is what I have so far:
Code:
typedef struct patient
{
BSTR Lname;
BSTR Fname;
char Mname;
BSTR Bdate;
BSTR Ndate;
int Upc;
}p;
int cmp(const void*,const void *);
int main(){
struct patient *first,*a,*b;
FILE *in,*out;
int nelem;
struct stat statbuf;
struct stat *s;
s=&statbuf;
in = fopen("patient.dat","rb");
stat("patient.dat",s);
first = (p*) malloc( s->st_size );
fread(first,s->st_size,1,in);
nelem = (int)(s->st_size / (long)sizeof(p) );
qsort(first,nelem,sizeof(p),cmp );
//need to modify code below to run through
//struct array and write out only the UPI's of the
//sorted array.
out=fopen("patient_sorted.dat","wb");
fwrite(first,sizeof(p),nelem,out);
fclose(in);
fclose(out);
free(first);
return 0;
}
As you can see, I am using qsort... which I was advised to use to sort these. So here's my compare function:
I need to compare the last names, if they are equal:
compare the first names, if they are equal:
compare middle initals, if equal:
compare birthdates, if equal:
compare UPI's (which are never equal),
If any of the above are not equal, it returns a value to qsort.
Custom compare function I wrote for qsort
Code:
int cmp(const void *a, const void *b){ //custom compare routine
int j;
char tmp[40],tmp1[40];
char *working,*working1;
struct patient *s;
struct patient *t;
zout(tmp);
zout(tmp1);
s = (struct patient*)a;
//the below code converts bstr into a vector passing it
//to safearray working? But then how do I strcomp then?
HRESULT i = VectorFromBSTR(s->Lname,working);
//below code does the same as above code for 2nd patient
t=(struct patient*)b;
i = VectorFromBSTR(s->Lname,working1);
j =strcmp( ? );
//compare last names
if j < 0 || j > 0 'if last names are not equal
return j;
if j == 0
{
//compare first names
//need code here to set tmp,tmp1 to fnames
j=strcomp(tmp,tmp1)
if j < 0 || j > 0 'if first names are not equal
return j;
if j == 0
{
//compare middle initials
//need code here to set tmp,tmp1 to middleinitial
j=strcomp(tmp,tmp1)
if j < 0 || j > 0 'if middle init's are not equal
return j;
if j == 0
{
//compare birthdates **the dates are stored as strings remember
//ie. 11/20/2000 or 02/31/1995
//need code here to set tmp,tmp1 to bdates
j=strcomp(tmp,tmp1)
if j < 0 || j > 0 'if birthdates's are not equal
return j;
if j == 0
{
//compare UPI (unique patient identifier, which is an integer)
//no two UPI's are the same, so one will
//always be greater or lesser than another
return j;
}//end UPI comparison
}//end birthdate comparison
} // end middle initial comparison
}//end firstname comparison
} //end lastname compare
}//end custom compare function