FSCANF format string issue
The following file has the following data in it. Each record (containing various fields seperated by commas, ends with a semi-colon).
a3.dat
Code:
123423454567987,29873,James,Harry,St. Louis,416-555-5555;223423454567987,198745,Jones,Beth,Toronto,416-555-5556;323423454567987,2349,Ng,Wei,Montreal,416-555-5557;423423454567987,9234617,Woo,Charles,Winnipeg,416-555-5558;523423454567987,2534,DeJesus,Pancho,Edmonton,416-555-5559;623423454567987,543876,Smith,Bob,Charlottetown,416-555-5544;723423454567987,1234,Kasim,Vladislov,Halifax,416-555-5566;823423454567987,98765,Yamaha,David,Vancouver,416-555-5577;923423454567987,26486,Lee,Jim,Calgary,416-555-5588;113423454567987,83456,Baker,Susan,St. Louis-de-ha-ha,416-555-5599;133423454567987,29873,James,Harry,St. Louis,416-555-5555;
The following function of mine has the following code.
Code:
void Bank::workFP(FILE* fp)
{
char accountTemp[15];
int balanceTemp = 0;
char stringTemp[315];
if(fp != NULL){
fscanf(fp, "%15[^,],%d[^,],%315[^;];", accountTemp, &balanceTemp, stringTemp);
printf("%d\n", balanceTemp);
puts(accountTemp);
puts(stringTemp);
}
else printf("Unable to open file.\n");
}
I am testing my code to see if it can print out the first record but I am having trouble with my fscanf format string.
Each record in the file has the following format: "accountNumber,balance,lastName,firstName,city,phon eNumber;" Each record is guaranteed to be no longer than 350 characters. "balance" is no longer than 20 characters, "accountNumber" is exactly 15 characters and the total number of characters in "lastName,firstName,city,phoneNumber;" is no longer than 315 characters.
With the above code, I recieve the output
Code:
29873
123423454567987
☺
Where the box is a random garbage character.
As seen in the first record
Code:
123423454567987,29873,James,Harry,St. Louis,416-555-5555;
The balance and accountnumber are correct, although I cant display the last group "James,Harry etc".
I believe this has something to do with my fscanf format string.
Appreciate any help in advance. Thanks.