在C 中进行文件处理以读取两个整数

File handling in c++ to read two integers

本文关键字:整数 两个 读取 处理 文件      更新时间:2023-10-16

如何从c 中逗号分隔的文件中读取两个整数?例如5,6写在文件中,我如何读这两个整数?

    /* sscanf example */
    #include <stdio.h>
    int main ()
    {
      char sentence []="Rudolph is 12 years old";
      char str [20];
      char str2 [20];
      int i;
      sscanf (sentence,"%s %s %d",str,str2,&i);
      //str=Rudolph 
      //str2=is
      //i=12
      printf ("%s %s -> %dn",str,str2,i);
      return 0;
    }

输出:鲁道夫是 -> 12

/* sscanf example */
#include <stdio.h>
int main ()
{
  char sentence []="5,6,7";
  char str [20];
  char str2 [20];
  int i,j,k;
  sscanf (sentence,"%d %*c %d %*c %d",&i,&j,&k);
  printf ("%d %d %dn",i,j,k);
  return 0;
}

输出:5 6 7