Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

different results every time

Status
Not open for further replies.

AndyDoyle

Programmer
Jan 31, 2003
5
US
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.
 
1) Is 104 the size of your biggest record?
2) Are all the structure members strings?
3) How are you writing the data to the file? Are they all fixed size or is it variable size?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top