質問

I created a file pointer array and I get core dump. If I write everything into one file, then my program works fine. What is the reason for this?

This works.

unsigned char error_array[4][4][256]
FILE *hypo_table;
hypo_table = fopen("00.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
            fprintf(hypo_table, "%.2x ", error_array[i][j][hypo_key]);
            if(hypo_key == 255)
                break;

This does not work (core dump).

unsigned char error_array[4][4][256]
FILE *hypo_table[4][4];
hypo_table[0][0] = fopen("00.txt", "w");
hypo_table[1][0] = fopen("10.txt", "w");
hypo_table[2][0] = fopen("20.txt", "w");
hypo_table[3][0] = fopen("30.txt", "w");

hypo_table[1][0] = fopen("10.txt", "w");
hypo_table[1][1] = fopen("11.txt", "w");
hypo_table[1][2] = fopen("12.txt", "w");
hypo_table[1][3] = fopen("13.txt", "w");

hypo_table[2][0] = fopen("20.txt", "w");
hypo_table[2][1] = fopen("21.txt", "w");
hypo_table[2][2] = fopen("22.txt", "w");
hypo_table[2][3] = fopen("23.txt", "w");

hypo_table[3][0] = fopen("30.txt", "w");
hypo_table[3][1] = fopen("31.txt", "w");
hypo_table[3][2] = fopen("32.txt", "w");
hypo_table[3][3] = fopen("33.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
            fprintf(hypo_table[i][j], "%.2x ", error_array[i][j][hypo_key]);
            if(hypo_key == 255)
                break;
役に立ちましたか?

解決

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. 
                  } 
               }
          }

他のヒント

You should recognize the fact that fopen can fail. There can be an upper limit on the number of files a process can open.

If fopen fails it returns NULL.

If it returns NULL, fprintf will access violated

hypo_table[3][3] = fopen("33.txt", "w");  assert(hypo_table[3][3] != NULL);

If you toss asserts in there for each one of them, you will see which one is failing. You will need to #include to be able to use the assert function.

Normally when you open a file, you should follow something along the line of this pattern:

FILE *fp = fopen("some_file.txt", "rw");
if (fp != NULL)
{
    /* Do file stuff */

    fclose(fp);
}
else
{
    printf("golly gee wilkers, the file didn't open\n");
    /* and maybe look at the errno variable to figure out why */
}

1) There's no reason you shouldn't be able to open 16 files concurrently.

2) Your 2-D array hypo_table looks OK.

3) We don't know anything about your 3-Day array error_array. I suspect that's where your problem is.

4) You should definitely check for all possible error conditions - like "fopen()" failing.

5) You should definitely step through the code under a debugger - this will help you determine exactly where it's failing, and why.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top