简体   繁体   English

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

[英]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.所以我试图想办法从 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]我想要数组[5]

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

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

arrayc [5] = {4 5 7 8, 4 5 6 7, 7 9 9 3, 7 3 6 3}数组c [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:假设您已经为 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.

相关问题 我如何使该文件的每一行的第一个数字递增? 那第一行不被跳过呢? 在C中 - How do I get this file the first number of each line to increment it? And what about the first line not being skipped? In C 在main()之前,C / C ++程序有什么方法可以崩溃吗? - Is there any way a C/C++ program can crash before main()? 有什么设计工具可以让我确切地获得C代码中不同功能的指令数量 - Is there any design tools that I can get the exact number of instructions of different functions in C code 如何求和 Matrix 的行而忽略第一行? C - How can I sum the rows of Matrix ignoring the first line? In C 如何在C程序中获得卷的可用空间? - How can I get free space of a volume in a C program? 如何摆脱 C 中的空白行? - How to get rid of a white space line in C? 回溯时如何获取文件名和行号? - How can I get the filename and line number when Backtracing? 有什么方法可以在C中制作“受保护”的typedef吗? - Is there any way I can make “protected” typedef's in C? 如何从 C 中的数字中获取每个数字? - How to get every digit from a number in C? 如何使文件中每行的第一个字母大写为C - How to make the first letter of every new line in a file as capital in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM