I have a file sort program thats causing me some confusion?
Every time I run it I'm getting different results?
I am opening the file allocating memory then reading the data to memory like so.
void memory_write(union Recs *mem)
{
char temp[104];
rewind(d_ptr);
while(fgets(temp,104,d_ptr))
{
switch (temp[0])
{
case 'I' :
case 'R' : mem->IRStr.RecordType = temp[0];
strncpy(mem->IRStr.CustCode,temp+1,5);
mem->IRStr.CustCode[6]='\0';
strncpy(mem->IRStr.PartNo,temp+1+5,6);
mem->IRStr.PartNo[7]='\0';
strncpy(mem->IRStr.Qty,temp+1+5+6,4);
mem->IRStr.Qty[5]='\0';
break;
case 'D' : mem->DStr.RecordType = temp[0];
strncpy(mem->DStr.CustCode,temp+1,5);
mem->DStr.CustCode[6]='\0';
break;
case 'C' : mem->CStr.RecordType = temp[0];
strncpy(mem->CStr.CustCode,temp+1,5);
mem->CStr.CustCode[6]='\0';
strncpy(mem->CStr.CustName,temp+1+5,20);
mem->CStr.CustName[21]='\0';
strncpy(mem->CStr.CustAddress,temp+1+5+20,60);
mem->CStr.CustAddress[61]='\0';
strncpy(mem->CStr.CustBal,temp+1+5+20+60,9);
mem->CStr.CustBal[10]='\0';
strncpy(mem->CStr.CreditLimit,temp+1+5+20+60+9,7);
mem->CStr.CreditLimit[8]='\0';
break;
}
mem++;//increment pointer to memory
}
return;
}
When its in memory I'm sorting using qsort.
However Sometimes it works and other times it doesn't?
eg.
The correct amount of characters are written into memory on some occasions, and on other times each string contains extra chars and there fore messes up the read.
Every time I run it I'm getting different results?
I am opening the file allocating memory then reading the data to memory like so.
void memory_write(union Recs *mem)
{
char temp[104];
rewind(d_ptr);
while(fgets(temp,104,d_ptr))
{
switch (temp[0])
{
case 'I' :
case 'R' : mem->IRStr.RecordType = temp[0];
strncpy(mem->IRStr.CustCode,temp+1,5);
mem->IRStr.CustCode[6]='\0';
strncpy(mem->IRStr.PartNo,temp+1+5,6);
mem->IRStr.PartNo[7]='\0';
strncpy(mem->IRStr.Qty,temp+1+5+6,4);
mem->IRStr.Qty[5]='\0';
break;
case 'D' : mem->DStr.RecordType = temp[0];
strncpy(mem->DStr.CustCode,temp+1,5);
mem->DStr.CustCode[6]='\0';
break;
case 'C' : mem->CStr.RecordType = temp[0];
strncpy(mem->CStr.CustCode,temp+1,5);
mem->CStr.CustCode[6]='\0';
strncpy(mem->CStr.CustName,temp+1+5,20);
mem->CStr.CustName[21]='\0';
strncpy(mem->CStr.CustAddress,temp+1+5+20,60);
mem->CStr.CustAddress[61]='\0';
strncpy(mem->CStr.CustBal,temp+1+5+20+60,9);
mem->CStr.CustBal[10]='\0';
strncpy(mem->CStr.CreditLimit,temp+1+5+20+60+9,7);
mem->CStr.CreditLimit[8]='\0';
break;
}
mem++;//increment pointer to memory
}
return;
}
When its in memory I'm sorting using qsort.
However Sometimes it works and other times it doesn't?
eg.
The correct amount of characters are written into memory on some occasions, and on other times each string contains extra chars and there fore messes up the read.