繁体   English   中英

有什么方法可以在 C 中的空格之前获得每行的第一个数字?

[英]Is there any way I can get the first number of every line before a space in C?

所以我试图想办法从 C 的文件中获取每行的第一个数字。

例子;

100 2 4 5 7 8

102 3 4 5 6 7

400 6 7 9 9 3

420 5 7 3 6 3

我想要数组[5]

数组[5] = {100, 102, 400, 420}

数组b[5] = {2, 3, 6, 5}

数组c [5] = {4 5 7 8, 4 5 6 7, 7 9 9 3, 7 3 6 3}

到目前为止,我有一种计算行数的方法;

  while ((c = fgetc (file)) != EOF)
  {
    if(c=='\n'){
      nlines++;
    }
  }

谢谢

假设您已经为 array/arrayb 和 arrayc 分配了足够的空间:

  int tmp[6]={0};
  int count=0;
  while (!feof(file)) {
      if(fscanf(file, "%d %d %d %d %d %d", tmp,tmp+1,tmp+2,tmp+3,tmp+4,tmp+5)==6){
          array[count] =tmp[0];
          arrayb[count]=tmp[1];
          arrayc[(count<<2)]  =tmp[2];
          arrayc[(count<<2)+1]=tmp[3];
          arrayc[(count<<2)+2]=tmp[4];
          arrayc[(count<<2)+3]=tmp[5];
          count++;
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM