为什么我的C程序的文件输出缺少最后一行与控制台相同内容的输出?

时间:2012-09-13 00:25:48

标签: c file-io

我正在使用以下代码。

#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行。我无法在这里找出问题。

1 个答案:

答案 0 :(得分:1)

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

显然,每个换行符都会自动刷新控制台输出(典型值);因此它们之间存在差异。

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

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