如何识别从scanf的字段分隔符? [英] how to identify a field separator from scanf?

查看:498
本文介绍了如何识别从scanf的字段分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读已与数名类似下面的文本文件。

I need to read a text file which has names with numbers like below.

rENLAg:12182
TgAlKd:19773
SSqUpz:16466
QYStPh:4113
CodNhz:28920
SgoIGz:25343

我需要的字母从numbers.fscanf只分离标识的空间,而不是其他领域的分隔符。因此,我该怎么办呢?

I need to separate the letters from the numbers.fscanf only identifies spaces, but not other field separators. Therefore how do I do it?

推荐答案

一个解决方案是使用的扫描设置(请参阅进入的转换说明 [设置] 格式说明表):

One solution would be to use scan sets (see the entry for Conversion specifier [set] in the format specifiers table):

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)
{
}

其中,%6 [^:]表示跳过任何前导空格(读取例如previous换行字符),并宣读了,但不包括第一个字符,但不超过6个字符(至prevent缓冲区溢出)

where " %6[^:]" means skip any leading whitespace (new-line character from previous read for example) and read up to, but not including, the first : character but no more than 6 characters (to prevent buffer overrun).

这篇关于如何识别从scanf的字段分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆