我需要阅读一个文本文件,其名称如下所示.
rENLAg:12182
TgAlKd:19773
SSqUpz:16466
QYStPh:4113
CodNhz:28920
SgoIGz:25343
Run Code Online (Sandbox Code Playgroud)
我需要将数字与数字分开.fscanf仅标识空格,但不标识其他字段分隔符.因此我该怎么做?
一种解决方案是使用扫描集(请参阅格式说明符表中的转换说明符 [set]条目):
char buf[7];
int i;
/* Check result of fscanf(), which returns the number
of assignments made, to ensure both 'buf' and 'i'
were assigned values. */
if (fscanf(fp, " %6[^:]:%d", buf, &i) == 2)
{
}
Run Code Online (Sandbox Code Playgroud)
其中" %6[^:]"表示跳过任何前导空格(例如前一个读取的换行符)并读取但不包括第一个:字符但不超过6个字符(以防止缓冲区溢出).