1

this is my c++ code

const int num_of_file = 1024;
std::ifstream data("data.txt");
std::vector<std::ofstream> files(num_of_file);
for (int i = 0; i < num_of_file; ++i)
{
    files[i].open(std::to_string(i) + ".txt");
    if (files[i].is_open() == false)
    {
        std::cerr << "open " << std::to_string(i) << ".txt fail" << std::endl;
        exit(0);
    }
}

but i received "open 509.txt fail" when i run the code every time.

Wonter
  • 293
  • 1
  • 5
  • 15
  • 3
    What platform are you using? I believe on many platforms the number of concurrently open file descriptors is severely limited. – static_rtti Mar 24 '17 at 11:03
  • If you need to have that many files open at once, are you sure you are going about solving your problem the best way? – crashmstr Mar 24 '17 at 12:06
  • More info here http://stackoverflow.com/questions/870173/is-there-a-limit-on-number-of-open-files-in-windows – Kenny Ostrom Mar 24 '17 at 12:26

1 Answers1

1

After a little bit of research the limitation seems to stem from the stream. stream is built on top of C "streams" (fopen, fread, etc), and those functions use collection of shared tables of "file handles", those tables having a maximum size that's "burned into" the VC++ runtime library. I'm a bit surprised that you're hitting the limit at 509 files - near as I can tell, the limit should be closer to 2048 files - but I'd bet that's the limit you're hitting.

You should keep an internal buffer and after a certain limit has been reached open, write and close the file.

Thrawn
  • 214
  • 5
  • 12