c - 将一些数据写入文件后出现段错误

标签 c file io printf

我正在编写一个 C 程序,它计算 94000 个图像区域的度量并将这些值写入文件中。我可以成功编写 51069 个区域的度量,但之后 C 程序向我输出段错误。代码很简单:

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

  Image *cimg=NULL;
  Image *mask=NULL;
  Image *gt=NULL;
  Image *rmask=NULL;
  Image *nwcimg=NULL;
  Image *nwrmask=NULL;
  Histogram *hist=NULL;
  FILE* output; 

  int x, r, j, y, maxR, minR, index, hsize, value, relevance;
  int min_x, min_y, max_x, max_y;

  //just checking the parameters
  if (argc != 5) 
  {
      fprintf(stderr,"usage: generatemetric<image> <region mask> <ground truth> <feature_file> \n");
      exit(-1);
  }  


    //reading the input image 
    cimg = ReadImage(argv[1]);

    //reading the image segmentation
    mask = ReadImage(argv[2]);

    //reading the ground truth which i will use to label the image regions as +1 and -1
    gt = ReadImage(argv[3]);

    //maximum and minimum region value
    maxR = MaximumValue(mask);
    minR = MinimumValue(mask);

    //i will iterate through all the image regions
    for(r=minR; r <= maxR; r++) 
    {  

            //this is done because I want to know 2 points in the image region
            //to create a bounding box on it
            min_x = mask->ncols-1;
            min_y = mask->nrows-1;
            max_x = 0;
            max_y = 0;


            for(y=0; y < mask->nrows; y++) 
            {
                 for(x=0; x < mask->ncols;x++)
                 {  
                    index = y*(mask->ncols)+x;

                    if(mask->val[index] == r) 
                    {
                          if(x < min_x)
                          min_x = x;
                          if(y < min_y)
                          min_y = y;
                          if(x > max_x)
                          max_x = x;
                          if(y > max_y)
                          max_y = y;
                     }
                   }
                 }

        nwcimg = CreateImage(max_x-min_x,max_y-min_y);
        nwrmask = CreateImage(max_x-min_x,max_y-min_y);

        //creating ROIS
        CreateROI(cimg, nwcimg, min_x, min_y, max_x, max_y);
        CreateROI(mask, nwrmask, min_x, min_y, max_x, max_y);                
        //calculating region class
        relevance = isRelevant(gt, mask, r);

        //makes the region of interest white in the bounding box
        make_binary(nwrmask,r);

      //calculates the metric only in the bounding box around the region of interest (a vector that is a kind of histogram)
        hist=metric(nwcimg,nwrmask);

       //starting to write

       //histogram size
       hsize = hist->n;

      //open the file to append the values
      //the problem starts here after 51069 iterations
      output = fopen(argv[4], "a+");

      //record the region class in the file         
      fprintf(output, "%d, ", relevance);

      //record each value of the histogram as comma separated values
      for(j = 0; j < hsize; j++) 
      {
            value = hist->v[j];
            fprintf(output, "%d, ", value);
      }
      //recod the region id
      fprintf(output,"%d \n", r+1);
      fclose(output);
  DestroyHistogram(&hist);
  DestroyImage(&nwcimg);
  DestroyImage(&nwrmask);

}
DestroyImage(&rmask);
DestroyImage(&gt);
DestroyImage(&cimg);
DestroyImage(&mask);
return(0);
}

换句话说,在每次 94000 次迭代中,程序都会打开文件、写入值并关闭它。我想程序运行时可能有很多 I/O。我对吗?或者是其他类型的问题?这里有人以前遇到过这个问题吗?怎么解决呢?

最佳答案

按照 user4815162342 的建议,通过在循环外打开文件并在循环后关闭文件来解决。我认为问题在于大量的 I/O,而且我大学的集群中也存在一些并发进程。程序立即运行完毕。感谢大家的关注和帮助。

关于c - 将一些数据写入文件后出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573058/

相关文章:

c - 将时间间隔结构化为可打印格式

c++ - 为什么 fseek 不起作用?

linux - Linux 上 POSIX AIO 和 libaio 的区别?

ruby - IO 对象的约定或经验法则?

c - i=i++ 会在 C17 中重新定义吗?

c - 现有连接被远程主机强制关闭 - MPI

c - 如何在C中实现链表?

java - Tomcat创建文件的路径

Python:确定一个对象是否类似于文件?

file - 什么是文件扩展名 .done?