This is a part of my 'Phonebook' program.
void refresh(){
char name[300][50], tmpa[50];
int i = 0, j, k, num[300], tmpb;
fp = fopen("Phonebook.txt","r");
while (!feof(fp)){
fscanf (fp, "%s %d", name[i], &num[i]);
++i;
}
for (j = 0; j <= i; ++j){
for (k = 0; k < i; ++k){
if ((strcmp(name[k], name[k+1])) > 0){
strcpy(tmpa, name[k+1]);
strcpy(name[k+1], name[k]);
strcpy(name[k], tmpa);
tmpb = num[k+1];
num[k+1] = num[k];
num[k] = tmpb;
}
}
}
fclose(fp);
fp = fopen("Phonebook.txt","w");
for (k = 0; k <= i; ++k){
fprintf(fp, "%s %d\n",name[k],num[k]);
}
fclose(fp);
menu();
}
It works just fine. It sorts the number in list just as I wanted. But when I ran the program, everything was sorted but on the top line of the file, there was this : 0 +8800
Why is this happening?