Thread: fopen failure

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    Angry fopen failure


    Hello everybody,

    Here is my problem.

    I wrote a program that does a statistical analysis, allocating large quantities of memories (3-400 MB) dynamically, and then performs cycles of calculations on the data.
    At the end of each cycle, the program opens 2 files in sequence and writes the results to the disk.

    With the first file there is no problem, with the second everything goes well for 20-30 cycles (on average, but the figure varies), when the fopen() that should open the file fails, returning a null pointer. If I try to make it open the same file (which is always the same file that it opened without problems for the first 20-30 cycles) passing to the fopen the name in “” instead of passing a variable, it still doesn’t open it.
    If I give it a wayout letting it open another file somewhere else, it succeeds and goes on.
    At the next cycle, it reopens the old file without problems.
    It is as the write permission on that file were temporarily altered in a cycle, to be brought back to their original values in the next cycle.

    The problem occurs with 3 different compilers (borland, Visual C++, mingw) and on 2 operating systems (windows NT e windows ME) , running on 2 different machines (both pentium)

    I tried to launch the program from the Visual C++ debugger with all the cheks activated, putting also checks on the heap (functions _CrtMemCheckpoint, _ASSERTE( _CrtCheckMemory( ) )), but apparently there is no memory corruption problem.

    Here is the function savememory where the error occurs.

    <pre>

    //------------------------------------------------------------------------------
    // FNCT: Sieve::savememo
    //------------------------------------------------------------------------------
    void Sieve::savememo(void)
    {
    int lr,nd,i,j,b,var1,* fp2;
    Node * ndp;

    fp = fopen(MEMORYFILE,"w");

    fprintf(fp,"\n%d",cyc);
    for (lr=1; lr<LAYERS; lr++) {
    fprintf(fp,"\n\nLR %d",lr);
    for (nd=0; nd<nn[lr]->dimlr; nd++) {
    ndp = nn[lr]->nodes[nd];
    fprintf(fp,"\n%3d) ; %.3f ;",nd,ndp->score);
    for (i=0; i<MAXIPNDS; i++) {
    fprintf(fp," [%3d", ndp->memo.inar[i].ndnr);
    fprintf(fp," %5.2f", ndp->memo.inar[i].wht);
    fprintf(fp," (%3d", ndp->memo.inar[i].spc.x1);
    fprintf(fp," %3d)", ndp->memo.inar[i].spc.x2);
    var1 = (int)ndp->memo.inar[i].spon;
    fprintf(fp," %d] ;", var1);
    }
    fprintf(fp," [%5.2f -1] ;", ndp->memo.bias);
    fprintf(fp," (%2d %2d)", ndp->memo.a.x1,ndp->memo.a.x2);
    fprintf(fp," (%2d %2d)", ndp->memo.b.x1,ndp->memo.b.x2);
    }
    }

    if (fclose(fp))
    printf("Error closing file\n");

    #ifdef DEBUG2
    printf("AFTER WRITE MEMORYFILE I AM! \n");
    printf("\n%s %d\n", DNAMEMFILE, cyc);
    #endif
    fp = fopen(DNAMEMFILE,"w");
    #ifdef DEBUG2
    printf("\nfp: %p\n", fp);
    //fprintf(fp,"\n%d",cyc);
    if (fp == NULL)
    fp = fopen("C:\\ZEUG\\ZOS\\MPXVC\\IOX\\MPXDNA0.TXT","w");
    if (fp == NULL) fp = fopen("C:\\ZEUG\\ZOS\\MPXVC\\IOX\\MPXDNA2.TXT","w");
    if (fp == NULL) fp = fopen("C:\\ZEUG\\ZOS\\MPX\\IOX\\MPXDNA3.TXT","w");
    if (fp == NULL) fp = fopen("C:\\ZEUG\\ZOS\\FILE01.txt","wt");

    printf("AFTER fopen(DNAMEMFILE) I AM! \n");
    #endif

    for (lr=1; lr<LAYERS; lr++)
    for (nd=0; nd<nn[lr]->dimlr; nd++) {
    fprintf(fp,"\n\nlr %d nd %d dimpop %d\n",lr,nd,
    nn[lr]->nodes[nd]->popp->dimpop);
    for (j=0; j<DIMPOP; j++) {
    fprintf(fp,"\n%5.3f %5.3f",nn[lr]->nodes[nd]->popp->guys[j].score,
    nn[lr]->nodes[nd]->popp->guys[j].dd);
    for (b=0; b<DIMDNA; b++) {
    var1 = (int)nn[lr]->nodes[nd]->popp->guys[j].dna[b];
    fprintf(fp," %d",var1);
    }
    }
    }

    fclose(fp);
    #ifdef DEBUG2
    printf("AFTER WRITE DNAMEMFILE I AM! \n");
    #endif

    }

    </pre>


    Help, I’m getting crazy!

    Thank you

    Alex
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    Have a look at the FlushFileBuffers() function (as your target is MS OS's)

    Try using CreateFile() (with the no buffering flag set) as fopen() calls this function anyway

    try using GetLastError() as soon as the file open call fails to find out why it fails.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0
    Technofear,

    I tried to follow your advice. I placed a GetLastError right after the test on the pointer of the fopen() and the result is error code = 32, which on my system means: "impossible to access the file, file is used by another process."

    Now, I try to figure out some possibilities:

    1. Is it possible that some system processes or not-system processes (I am thinking at the Kazaa stuff which I once downloaded - now I deleted it but I notice that, when I connect to the internet, an icon "connected" in the Kazza style appears at the bottom of the screen).

    2. The file was not closed properly at the last cycle and so it is still marked as "used". In this case should be seen as used bu the same process, not by another one.
    Now I am going to check out the result of the second fclose();

    What do you think?

    Alex

IMN logo majestic logo threadwatch logo seochat tools logo