Below is part of my code. address[] is an array of 100 characters
I would like to ask how do I correctly terminate the address[] array so that when i call puts(address) only the current address is shown.
Because now, whenever I sscanf an address, there is a problem. Even though I have already placed '\n' and '\0' at the end of the array, when I use puts(address) I get the current address followed by remnants of the previous address in the loop.
Example :
45 5th Street <----- 1st address
21 Hell <--- 2nd address
when I puts(address) I will see
21 Hell Street
Even though I placed '\0' at the end of hell.
Thanks.
Pier.
Code:
while (fgets(buffer, 100, fp)!=NULL) {
if ((strcmp(buffer, "\n"))!=0) {
switch (counter) {
case 1 : sscanf(buffer, "Account Id = %d", &accountID);
break;
case 2 : sscanf(buffer, "Name = %79c", name);
name[strlen(name)] = '\0'; /*null terminate the string*/
break;
case 3 : //for (i=0; i <= 100; i++) address[i] = ' '; works when I include this but not veri efficient, i'm sure there's another way
sscanf(buffer, "Address = %99c", address);
printf("LENGTH = %d\n", strlen(address));
address[strlen(address)-1] = '\n';
address[strlen(address)] = '\0';
break;
case 4 : sscanf(buffer, "DOB = %2c-%2c-%4c", dobDay, dobMth, dobYr);
dobDay[2] = '\0';
dobMth[2] = '\0';
dobYr[4] = '\0';
break;
case 5 : sscanf(buffer, "Phone Number = %8c", phoneNo);
phoneNo[8] = '\0';
break;
case 6 : sscanf(buffer, "Account Balance = %lf", &balance);
break;
}
if (counter==6) {
printf("%d\n", accountID);
puts(name);
puts(address);
puts(dobDay);
puts(dobMth);
puts(dobYr);
puts(phoneNo);
printf("%lf\n", balance);
counter = 1; /*reset*/
}
else counter++;
for (i=0; i <= 100; i++) buffer[i] = ' ';
}
}