0

我正在使用以下代码。

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

int main(void)
{   
 int two=0, two2 = 0;
 int cast11, cast12,c1157,c1257;
 static const char filename[] = "clearedacg_filter.txt";
 static const char filename2[] = "FinalVariantReferenceTable_FilterHomVarArcLiverDR_sorted.txt";
 char* chrarray[22] = {"chr1","chr2","chr3","chr4","chr5","chr6","chr7","chr8","chr9","chr10","chr11","chr12","chr13","chr14","chr15","chr16","chr17","chr18","chr19","chrX","chrY","chrM"};    
 FILE *file3 = fopen("AllSNPcounts_filter.txt","w");
 FILE *file4 = fopen("AllSNPcounts_nonhits.txt","w");
 char word[3];
 char temp[] = "chr1";
 char* dp4 = "DP4";
 FILE *file = fopen(filename, "r");
 unsigned int i,j,k,sum;
 int count = 0;
 int dp4_field = 0;

 FILE *file2;
 file2 = fopen(filename2, "r");
 if ( file != NULL )
 {
  char line[BUFSIZ],line2[BUFSIZ];
  char one[20], three[22], five[100], four[20], four2[20], one2[20], three2[22], five2[100], six[20], six2[20], seven[20], seven2[20];

  fgets(line, sizeof line, file); 
  fgets(line2, sizeof line2, file2);

  while(1)
  {  
   sscanf(line, "%19s %21s %*s %19s %19s %19s %*s %99s", one, three, four, six, seven, five);
   two = atoi(three);
   sscanf(line2, "%19s %21s %*s %19s %19s %19s", one2, three2, four2, six2, seven2);
   two2 = atoi(three2);
   if(!strcmp(temp,one))
   {
    if(!strcmp(temp,one2))
    {
     if(two == two2)
     {
      dp4_field = 0; 
      for(i=5; i < strlen(five);i++)
      {
       sum = 0;
       for(j=i,k=0; k<3;j++,k++)
       {
        *(word+k) = five[j];
       }
       if(strcmp(word,dp4) == 0)
       {
        dp4_field = 1;
        sscanf(five+4+i, "%d,%d,%d,%d",&cast11 , &cast12, &c1157, &c1257);
       }

      }
      if(dp4_field)
      {
       printf("%s\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t\n", one2, three2, cast11+cast12, c1157+c1257, four, six, seven, four2, six2 , seven2);
       fprintf( file3,"%s\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t\n", one2, three2, cast11+cast12, c1157+c1257, four, six, seven, four2, six2 , seven2);
      }
      if(fgets(line2, sizeof line2, file2) == NULL)
       break;
      if(fgets(line, sizeof line, file) == NULL)
       break;
     }

     else if(two > two2)
     {
      fprintf( file4,"%s\t \n", line2);
      if(fgets(line2, sizeof line2, file2) == NULL)
       break;
     }

     else 
     {
      if(fgets(line, sizeof line, file) == NULL)
       break;
     }

    }
    else
    {
     count++;
     strcpy(temp,chrarray[count]);
     while(strcmp(temp,one))
     {
      if(fgets(line, sizeof line, file) == NULL)
       break;

      sscanf(line, "%19s %21s %*s %19s %19s %19s %*s %99s", one, three, four, six, seven, five);
     }
    }
   }
   else
   {
    count++;
    strcpy(temp,chrarray[count]);
    while(strcmp(temp,one2))
    {
     if(fgets(line2, sizeof line2, file2) == NULL)
      break;
     fprintf( file4,"%s\t \n", line2);
     sscanf(line2, "%19s %21s %*s %19s %19s %19s %*s %99s", one2, three2, four2, six2, seven2, five2);  
    }
   }
  }
  fclose(file);
  fclose(file2);
  fclose(file3);
  fclose(file4);
 }
 else
 {
  perror(filename);
 }
 return 0;

}

我正在控制台上打印并打印到此代码中的文件中。它在控制台上显示良好,但在打印到文件时跳过了最后几行,如 20 到 40 行。我无法弄清楚这里的问题。

4

1 回答 1

1

您有一个简单的错误,显然是在没有执行fclose底部调用集的情况下退出您的程序。您永远不会显式刷新输出,因此它通常只会在您最后关闭文件流时刷新最终缓冲的内容,但如果程序失败(或挂起),则不会调用这些内容。

对于每个换行符(典型),控制台输出显然会自动刷新;因此它们之间的区别。

当然,真正的问题不是缺少刷新,而是导致程序失败或挂起的错误。如果这是你认真编码的东西,而不是家庭作业或测试问题,那么你应该能够找出问题所在,或者通过它进行调试并解决它。我不会为你做那部分。;)

补充:但我会给你一个提示:数组是有限的,如果你访问它们的末尾,就会发生坏事。

于 2012-09-13T19:30:36.890 回答