The mistake you are doing is you are using uninitialized streams in loop. you are opening same files twice.But you did not opened some files and you did not assign some streams
hypo_table[0][0] = fopen("00.txt", "w");
Here You did Not opened files 01.txt
,02.txt
, and 03.txt
And
hypo_table[0][1],hypo_table[0][2],hypo_table[0][3]
are not valid streams
hypo_table[1][0] = fopen("10.txt", "w"); //Here ,it is 01
hypo_table[2][0] = fopen("20.txt", "w"); //Here ,it is 02
hypo_table[3][0] = fopen("30.txt", "w"); //Here ,it is 03
hypo_table[1][0] = fopen("10.txt", "w"); //Here You are reopening
hypo_table[1][1] = fopen("11.txt", "w");
hypo_table[1][2] = fopen("12.txt", "w");
hypo_table[1][3] = fopen("13.txt", "w");
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
for(hypo_key = 0; hypo_key < 256; hypo_key++) {
//process error_array
Here you are trying to access invalid streams. this will give segfault.
fprintf(hypo_table[i][j], "%.2x ", error_array[i][j][hypo_key]);
Use loop to open files and assign streams And check return value of fopen()
based on return value proceed further.
Use sprintf()
to create filename strings.
FILE *hypo_table[4][4],*fp=NULL;
char buf[10];
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
sprintf(buf,"%d%d.txt",i,j);
fp = fopen(buf, "w");
if(fp!=NULL)
{
hypo_table[i][j] =fp;
//You can include inner loop Here
fp=NULL;
}
else
{
perror("ERROR");
//Handle As you want or simply exit.
}
}
}