openlang

Opening files on Raspberry Pi 4

On a Raspberry Pi 4 I use fopen inside a for loop to read files from a particular directory, over 1200 of them. Once the loop has run enough times, fopen stops being able to read another file. What should I do about this?

The conversation continues 2

Follow the reasoning
  1. Permalink

    Once the loop has run enough times, fopen stops being able to read another file.

    My guess would be that you forgot to fclose files once you've finished with them, eventually using up memory or running out of file descriptors.

    What should I do about this?

    Remember to call fclose on those files.

  2. Permalink

    Opening a file with fopen makes the system assign a file descriptor for it. The supply of those descriptors is limited. From a brief google search, microsoft apparently normally offers 640 file descriptors. The file also gets brought into memory. Opening many files can therefore exhaust your memory quite quickly. Close every file once you've finished using it. With just a handful of files this generally doesn't cause trouble. When you need thousands, as you do here, close each one immediately after its use is finished.

Other sides of the same question

  1. Does Go reserve SIGUSR1 and SIGUSR2?

    Dear programmers, we want to execute a go application under dmtcp (GitHub hosts dmtcp/dmtcp, the DMTCP distributed multithrea…

    Replies 2Latest activity go
  2. How can an inets server be stopped?

    Hello, I've got a fairly elementary question about inets (which, incidentally, I find quite handy because it needs no additio…

    Replies 2Latest activity erlang