C: 扫描的双数组值为什么变化?

标签 c arrays file double scanf

我正在扫描文件中的 double 组。 我可以打印此数组以检索文件中每一行的正确值。

在此过程中,当我尝试将另一个文件扫描到另一个数组时,我的问题就出现了。

我有:

//
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main (int argc, char **argv) {

  double c[13], d[13];
  char filename[20];
  double x1, x2, y1, y2, z1, z2, d1, d2, au, aux, auy;
  double dx, dy, dz, nnid, chk, nn;
  int n, b;
  char *in;
  in=argv[1];
  FILE* infile;
  FILE* inbfile;
  infile = fopen(in, "r");
  inbfile = fopen("copy.bt", "r");

  while (!feof(infile)) {

    fscanf(infile, "%f %f %e %e %e %e %e %e %e %e %e %e %e %f",c,c+1,c+2,c+3,c+4,c+5,c+6,c+7,c+8,c+9,c+10,c+11,c+12,c+13);
    printf("Selected Particle A: %f\n",c[0]);
    n=0;

    while (!feof(infile)) { 
      printf("%f\n",c[0]);
      fscanf(infile, "%f %f %e %e %e %e %e %e %e %e %e %e %e %f",d,d+1,d+2,d+3,d+4,d+5,d+6,d+7,d+8,d+9,d+10,d+11,d+12,d+13);
      printf("%f\n",c[0]);
      printf("Selected Particle B: %f\n",d[0]);

      /**/
      printf("%f = %f ?\n",c[0],d[0]);
      if (c[0]==d[0]) {
        printf("Same Particle SKIP...\n");
      }
      else {  

        dx = (d[4])-(c[4]);
        dy = (d[5])-(c[5]);
        dz = (d[6])-(c[6]);

        printf("dx dy dz %e %e %e\n",d[4],d[5],d[6]);

        /**/
        if (n == 0) {  
          au=pow(((dx*dx*dx)+(dy*dy*dy)+(dz*dz*dz)),(1.0/3.0));
          printf("%f is %e from %f\n",c[0],au,d[0]);
        }
        else { 
          aux=pow(((dx*dx)+(dy*dy)+(dz*dz)),(1.0/3.0));
          printf("%f is %e from %f\n",c[0],aux,d[0]);
          if (aux < au) {
            au = aux;
            nnid = d[0];
          }
        }
        /**/

      }
      n++;
      nn=d[1];
      /**/

    }

    printf("%f Is Particle %f At %e\n", c[1], nnid, au);

  }

  fclose(infile);
  fclose(inbfile);
  return 0;

}

那么为什么值会改变呢?我所做的只是将另一个文件的第一行扫描到一个单独的数组中。

最佳答案

您正试图将 14 个值读入具有 13 个元素的数组,导致未定义的行为。将数组声明为 float c[14], d[14]

当你将 d 声明为 float d[13] 时,d+12 指向数组中的最后一个元素,并且 d+13 超出了数组的末尾,因此不允许读取它。

关于C: 扫描的双数组值为什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671294/

相关文章:

php - 我有一个数组可以破坏error_log(),但不能破坏print_r(),并且在其他情况下可以完美地工作。

Java多客户端聊天应用程序-发送消息问题

javascript - 映射一个数组并根据对象键返回一个新数组

c - C 中的文件扩展名作为用户输入

c - "strlen(s1) - strlen(s2)"永远不会小于零

c - 如何在 C 中打印居中对齐的字符串

file - 如何使用golang检查网页文件是否存在?

java - IO方法 block :

c - 如何将 512KB 数据的 SHA1 与元信息文件中给定的 SHA1 进行比较?

c - 为什么 gets() 读取的字符比我在用 calloc() 初始化它时设置的限制更多?