Arrays 含阵列结构的断层分割

Arrays 含阵列结构的断层分割,arrays,structure,Arrays,Structure,我的程序需要从输入文件中读取值(整数和浮点)。一行中有许多值,用制表符分隔,文件中有许多行。我计划逐行读取整个文件,解析每一行,并将整个内容存储到由二维静态数组组成的结构中。然后,当整个内容存储在内存中时,我会进行计算,将结果填写到另一个结构中,最后将该结构的内容转储到输出文件中。当然,它不工作,我得到分段错误时,试图运行程序 我是这样定义输入结构的: #define sim_spectrum_constant 122 #define real_spectrum_constant 181 #de

我的程序需要从输入文件中读取值(整数和浮点)。一行中有许多值,用制表符分隔,文件中有许多行。我计划逐行读取整个文件,解析每一行,并将整个内容存储到由二维静态数组组成的结构中。然后,当整个内容存储在内存中时,我会进行计算,将结果填写到另一个结构中,最后将该结构的内容转储到输出文件中。当然,它不工作,我得到分段错误时,试图运行程序

我是这样定义输入结构的:

#define sim_spectrum_constant 122
#define real_spectrum_constant 181
#define num_days 365

  struct spectrum {
    int year[sim_spectrum_constant][num_days];
    int month[sim_spectrum_constant][num_days];
    int day[sim_spectrum_constant][num_days];
    int hour[sim_spectrum_constant][num_days];
    int minute[sim_spectrum_constant][num_days];
    int second[sim_spectrum_constant][num_days];
    float wavelength[sim_spectrum_constant][num_days];
    float irr_total[sim_spectrum_constant][num_days];
    float irr_direct[sim_spectrum_constant][num_days];
    float irr_diffuse[sim_spectrum_constant][num_days];
  };

  struct spectrum pS;
以下是我初始化阵列的方式:

// initialization
  for (i=0; i<sim_spectrum_constant; i++) {
    for (j=0; j<num_days; i++) {
      pS.year[i][j] = 0;
      pS.month[i][j] = 0;
      pS.day[i][j] = 0;
      pS.hour[i][j] = 0;
      pS.minute[i][j] = 0;
      pS.second[i][j] = 0;
      pS.wavelength[i][j] = 0.;
      pS.irr_total[i][j] = 0.;
      pS.irr_direct[i][j] = 0.;
      pS.irr_diffuse[i][j] = 0.;
    }
  }
//初始化

对于(i=0;i您似乎没有计算
sscanf
返回的值,它返回它已解析的参数数量,因此这将是一个有用的检查方法。通常检查返回值是一件好事

此外,如果您有具有固定维度的固定数组,那么您还应该检查边界,如n,不要变得太大。
n>=num_days
输入数据常常会让我们程序员感到惊讶,这是一种糟糕的方式

<>你的结构看起来过于复杂,结构的思想是使事情变得更简单和更有组织性,但是你把它当作是一种整块变量,你把所有的东西都倾倒了。你应该考虑里面有更多的结构,比如,日期可以是一个结构,从ike可能是结构的候选者

e、 g


这些数组是在什么地方初始化的吗?不是,是必须的吗?这是我十年来的第一个C程序,对不起:(我刚刚添加了初始化并重新编译,似乎仍然是同一个问题。我已经更新了上面关于init是如何完成的。你能调试一下以知道错误发生在哪一行吗?我确实尝试过,但是调试从未在程序中运行过。我几乎立即收到警告消息,“访问冲突(分段错误)[was]在计划中提出”.Debug之后似乎无法通过该程序。我还尝试在整个程序中设置控件printfs,每两行打印一个正确的数字,以便能够更准确地定位问题所在。但是,当我运行该程序时,甚至看不到第一次打印输出,这意味着程序ram实际上根本就没有启动?问题解决了!分段错误是由于阵列太大,堆栈无法容纳它们造成的。我首先尝试更改链接器参数以增加可用堆栈,但这没有帮助。然后我重新定义了我的结构,如下所示:static struct spectrum pS。…分割错误消失了。我还考虑了这里提供的其他建议,这肯定会使我的程序设计更好。感谢所有帮助我的人!问候
// determining how many simulated spectrums are provided in the file + reading spectrums into the array
  row = 0;
  n = 0;
  if (fgets(line, sizeof(line), pInput_spektrum) == NULL) {
    printf ("****Simulated spectrum doesn't contain any entries!! -- %s\nPress <enter> to exit...\n", strerror(errno));
    getchar();
    exit(-1);
  }

  while(fgets(line, sizeof(line), pInput_spektrum) != NULL) {
    result=sscanf(line, "%i %i %i %i %i %i %f %f %f %f", pS.year[row][n], pS.month[row][n], pS.day[row][n], pS.hour[row][n], pS.minute[row][n], pS.second[row][n], pS.wavelength[row][n], pS.irr_total[row][n], pS.irr_direct[row][n], pS.irr_diffuse[row][n]);
    row ++;
    if (row == sim_spectrum_constant) {
      n ++;
      last_row = row;
      row = 0;
    } 
  }
struct irr
{
  float total;
  float direct;
  float diffuse;
};