2

I am trying to write in multiple files using ofstream. The problem is that I'm not able to write to more than 1020 files at the same time. I don't understand if it's my laptop problem or if it's an internal limit of fstream.

I wrote a simplified version of my program. Obviously in this example it's not necessary to open multiple fstream channels.

#include <fstream>
#include <vector>

using namespace std; 

int main(){
  vector<ofstream> write(2000);
  char namefile[50];

  for(int i=0; i<2000; i++){
    sprintf(namefile,"text_%d.txt",i);
    write[i].open(namefile, ofstream::out);

    if (write[i].fail())
      printf("Error writing to %s\n", namefile);

    write[i] << "Hello!";
    write[i].flush();
  }

  for(int i=0; i<2000; i++)
    write[i].close();

  return 0;
}
Snaporaz
  • 133
  • 6
  • 4
    Most operating systems set a limit on the number of files that a single process or user can open concurrently. What operating system are you using? This has nothing directly to do with C++, which sets no such limit. –  Jan 17 '17 at 19:45
  • I'm running ubuntu 16.04. Is there any way to check and/or change such limit? – Snaporaz Jan 17 '17 at 19:48
  • 3
    http://stackoverflow.com/questions/34588/how-do-i-change-the-number-of-open-files-limit-in-linux –  Jan 17 '17 at 19:50
  • @latedeveloper now I feel like an idiot. I really was clueless about what to search for. Thanks. – Snaporaz Jan 17 '17 at 20:02

0 Answers0