2
格式化字符串作爲家庭作業,我需要加載以下格式的數據文件的一部分:的sscanf從文件
R1 Fre 17/07/2015 18.00 FCN - SDR 0 - 2 3.211
R1 Lor 18/07/2015 16.00 FCM - VFF 2 - 0 7.232
對於這樣我用與fgets字符串存儲在臨時字符串之後,sscanf在逐行遍歷文件的同時格式化字符串。
while(fgets(temp, MAX_LINE_SIZE, input_file)!= NULL) {
sscanf(temp,
" %*s %3s %d/%d/%d %s %3s - %3s %d - %d %6s",
round[i].match[j].weekday,
&round[i].match[j].day,
..... And so on ....
j++;
}
電流輸出是:
Weekday: Fre18.00FCNSDR3.211
Day: 17
Month: 7
Year: 2015
Start: 18.00FCNSDR3.211
Home team: FCNSDR3.211
Away team: SDR3.211
Score: 0 - 2
Viewers: 3.211
預期成果是:
Weekday: Fre
Day: 17
Month: 7
Year: 2015
Start: 18.00
Home team: FCN
Away team: SDR
Score: 0 - 2
Viewers: 3.211
與%s在sscanf的佔位符的字符串,似乎把自己在一起,出於某種原因。
所有幫助多讚賞。
你是完全正確的。我忘了爲每個字符串分配足夠的內存。 **謝謝!** – asdasd