【发布时间】:2014-11-15 03:03:55
【问题描述】:
使用istringstream,我们可以从一个字符串中一个一个地读取项目,例如:
istringstream iss("1 2 3 4");
int tmp;
while (iss >> tmp) {
printf("%d \n", tmp); // output: 1 2 3 4
}
我们可以使用sscanf 来做到这一点吗?
【问题讨论】:
标签: c++ scanf istringstream
使用istringstream,我们可以从一个字符串中一个一个地读取项目,例如:
istringstream iss("1 2 3 4");
int tmp;
while (iss >> tmp) {
printf("%d \n", tmp); // output: 1 2 3 4
}
我们可以使用sscanf 来做到这一点吗?
【问题讨论】:
标签: c++ scanf istringstream
你可以用这个非常接近地模拟它
const char *s = "1 2 3 4";
int tmp, cnt;
for (const char *p = s; sscanf(p, "%d%n", &tmp, &cnt) == 1; p += cnt)
printf("%d\n", tmp);
【讨论】:
%n 是否被sscanf 计算在内。我会改为测试>= 1。
%n 指令的执行不会增加在完成fscanf 函数的执行时返回的赋值计数。”
%n 不计算在内。此外,C99 明确指出 *(赋值抑制)不能与 %n(未定义的行为)一起使用,这也与链接中的文本相矛盾。我认为链接上的文字是一个维护/审查不佳的多个历史变化的综合体。
%n 的行为方式,就不可能编写正确的代码。
如果您可以使用strtok 将字符串分成单独的标记
char str[] ="1 2 3 4";
char * pch;
pch = strtok (str," ");
while (pch != NULL)
{
int tmp;
sscanf( pch, "%d", &tmp );
printf( "%d \n", tmp );
pch = strtok (NULL, " ");
}
否则,scanf 支持 %n 转换,可以让您计算到目前为止消耗的字符数(我还没有测试过,可能存在我没有考虑过的陷阱);
char str[] ="1 2 3 4";
int ofs = 0;
while ( ofs < strlen(str) )
{
int tmp, ofs2;
sscanf( &str[ofs], "%d %n", &tmp, &ofs2 );
printf( "%d \n", tmp );
ofs += ofs2;
}
【讨论】: