簡體   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