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