Paul Paul - 23 days ago 9
C++ Question

Is there a reason fopen() wouldn't work after several hundred opens?

Hey, for this piece of code, the person who wrote the system communicates data between processes using textfiles. I have a loops that looks (for all intents and purposes) like this:

while (true)
{
//get the most up-to-date info from the other processes
pFile = fopen(paramsFileName, "r");

// Do a bunch of stuff with pFile

Sleep(100);
}


This will work for several hundred times, but for whatever reason it will return NULL after a while, even though it has opened that same file path several hundred times already! I have double checked that the file exists and has data in it when the fopen returns NULL, and have tried to put a delay/retry in there to no effect.

What can you think of that would cause this?

NG. NG.
Answer

You're hitting the open file / file descriptor limit for your OS. It should run forever if you do fclose(pFile) in your loop.