次のコードを使用しています。
#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 行などの最後の数行がスキップされます。ここで問題がわかりませんでした。