2

I need to have more than 60 text files opened at the same time in my C program. However, it seems that fopen is not able to handle more than 60 files simultaneously. I am programming in Windows environment.

I use the following fopen statement:

fopen(fileName.c_str(),"wt");

Where fileName is the path of my txt file, name which changes inside a loop along 100 files. Does anybody know any trick to make this work? Or any alternative?

bardulia
  • 145
  • 11

2 Answers2

2

If you issue the bash shell command:

ulimit -n

you'll see that 60 is your limit for open file handles. You can change it with:

ulimit -n 256

Note: there're soft (-S) and hard (-H) limits you can see with -Sn and -Hn, you can change your soft limit up to your hard limit.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
2

There's actually two things that constrain how many files you can have open at any time:

  1. The environment limit specified by ulimit -n.
  2. The C runtime library. I know of several that limit you to 256 file handles (Sun to name one)

Your current limit is probably 63 once you take into account STDIN, STDOUT and STDERR already being opened, and I don't know of a system that goes that low so it's probably your ulimit but you need to be aware of the other limit.

On windows you can use _setmaxstdio(n) but in the default case you should still be able to open 512 files. so I'm still a little confused as to why you only get 60 odd unless you open each file about 8 times...

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61