Quote Originally Posted by wdolson View Post
That's odd, I wonder why the code tags didn't work? I can't post the complete code because it's both proprietary (owned by my client) and there are lots of layers to it. The loop that eventually calls this function is two function calls back from this one.

I didn't realize that the GetLastError() function was unreliable in the error code return, though the error code I got was file related.

The code for the file is not much more than I posted, since it was temporary debug code, I haven't gone through any extensive checks to make sure the file was opening correctly, etc. This code will be gone long before a customer sees it. I'll flesh it out a little further:
Code:
FILE *fptr=0;
    char filename[256]="DebugFile.txt";
    DWORD err;

    fptr=fopen(filename,"a+");
    if(fptr==0)
    {
        err=GetLastError(); //Just put a breakpoint here to see what the error was
    }

    if(flag)
    {
      ret=CallFuntionThatDoesSomethingToTheData();
       fprintf(fptr,"Some debug text ret=0x%x\n",ret);
      if(fptr)
        fclose(fptr);
    }
I put code to create log files in all three layers of code. When down on this layer, there would be three file handles open at once because the first function calls the second which calls this one.

When I removed the file code from the top function call, this began working without errors. I also fixed the bug, so I don't need it anymore, but it appears there was something with three file handles open at once that was causing the problem. It only happened after 1000 or so calls, never at the start. Even though all three file handles were always open when it got down to this function call.
GetLastError is not unreliable, you are not using it in the correct context. You cannot use this function to check the state of a CRT function call.

Viggy