简体   繁体   中英

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

So I was trying to think of a way to get the first number of each line from a file in C.

Example;

100 2 4 5 7 8

102 3 4 5 6 7

400 6 7 9 9 3

420 5 7 3 6 3

And I want to have the array[5]

array[5] = {100, 102, 400, 420}

arrrayb[5] = {2, 3, 6, 5}

arrayc [5] = {4 5 7 8, 4 5 6 7, 7 9 9 3, 7 3 6 3}

So far I have a way to count the number of lines;

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

Thank you

assume you have allocated array/arrayb and arrayc with sufficient space:

  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++;
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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