用sscanf迭代 [英] Iterating with sscanf

查看:58
本文介绍了用sscanf迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对sscanf函数了解不多,但是我想做的是遍历整数行.给定变量

I do not know much about the sscanf function, but what I am trying to do is iterate through a line of integers. Given the variable

    char *lineOfInts

我已经将此行注册了用户输入.我可以很好地输入,但是当我尝试使用sscanf时,我想遍历每个int.我知道,如果我知道像这样手前会有多少个整数,就可以对所有整数进行核算.

I have made this the line that registers the users input. I am able to get the input fine, but when I try to use sscanf, I want to iterate through every int. I know that I can account through all the ints if i know how many ints there will be before hand like this..

   sscanf(lineOfInts, "%d %d %d..etc", &i, &j, &k...) 

但是如果我不知道用户将输入多少个整数怎么办?我如何只用一个变量就可以计算所有整数?喜欢

but what if I don't know how many integers a user will input? How can I account for all of the integers with just one variable? like

   sscanf(lineOfInts, "%d", &temp);
   //modifying int
   // jump to next int and repeat

谢谢!

推荐答案

也许是这样的:

#include <stdio.h>

int main(void)
{
  const char * str = "10 202 3215 1";
  int i = 0;
  unsigned int count = 0, tmp = 0;
  printf("%s\n", str);
  while (sscanf(&str[count], "%d %n", &i, &tmp) != EOF) {
    count += tmp;
    printf("number %d\n", i);
  }

  return 0;
}  

这篇关于用sscanf迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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