2

I am trying to execute essentially the code below. I create an array of file streams and then open all of them and write something to them. The problem is that for some reason if balls is greater than 508, it doesn't create the files anymore... Yes, the number 508. Even if I try to manually create the 509th stream after the loop, it will not create it (does not appear the directory). This happens if I try to make balls any value greater than 508 and the only way I have found to get it to go past this is to close some or all of the previous streams. I really want to be able to have 2000 streams open through the duration of the program run time before closing them, but I am assuming this has something to do with a memory limit. How much space does it take to have a simple file stream open? I have an i7 3770k which has decent cache sizes, 64 bit machine, and 16G of memory. Note that this occurs even just with the x,y,z etc string being written and nothing else. It's a tiny file.

#include <fstream>
#include <iostream>

const int balls = //Whatever I want...;
std::ofstream ballWrite[balls];

int main(){
    // Open all file streams:
    for (int Ball = 0; Ball < balls; Ball++)
    {
        ballWrite[Ball].open("ball" + std::to_string(Ball) + ".csv", std::ofstream::app);
        ballWrite[Ball] << "x,y,z,w_x,w_y,w_z,w_mag,radius,mass,moi\n";
    }
}
Michael
  • 77
  • 6
  • 4
    This is mainly OS dependent. C++ doesn't spefify a particular limit. – πάντα ῥεῖ Jul 14 '16 at 06:01
  • 1
    For linux env, you have a great answer [\[ here \]](http://unix.stackexchange.com/a/36842/25919) . – sjsam Jul 14 '16 at 06:07
  • 1
    509+3 (the number of standard streams) = 512. Just something to note. – n. m. could be an AI Jul 14 '16 at 06:08
  • @n.m. I Was trying to find a way that this could be something like that, but I see no way that it is a power of 2. There are no other 3 things going on. – Michael Jul 14 '16 at 06:15
  • @sjsam I am on windows 10, but it looks like it really is just a "test it and see how many you have have open kind of thing". I guess this is an excuse to make less streams that hold more data. – Michael Jul 14 '16 at 06:17
  • @Michael _"There are no other 3 things going on. "_ You forgot `cin`, `cout`and `cerr`. – πάντα ῥεῖ Jul 14 '16 at 06:30
  • @πάνταῥεῖ I'm not doing any of those during this process. Also, is this a globally shared limit with any kind of stream? I have an array of stringstreams declared and filling with data as well... – Michael Jul 14 '16 at 06:33
  • @Michael These are instantiated automatically, no matter if you use them or not. As mentioned the 512 maximum is a limit of your operating system. – πάντα ῥεῖ Jul 14 '16 at 06:36
  • These strings are open automatically whether you are using them or not. Try closing them explicitly before doing other things. – n. m. could be an AI Jul 14 '16 at 06:39
  • What I am trying to say is that I would really like to have more than 508 streams open at once... Is this impossible? – Michael Jul 14 '16 at 06:47
  • Also, why did @Bo Perssons mark this post as a duplicate and not provide the duplicate? I don't even understand why that is allowed. – Michael Jul 14 '16 at 06:50
  • 1
    @Michael - The link to the duplicate is automatically shown right at the top, above the question. – Bo Persson Jul 14 '16 at 06:51
  • I was looking for it at the location of "marked as duplicate". Thanks. That did answer the question well. It also confuses me though because I have much more than 512 streams open if you count all of my stringstreams as well. I guess this only applies to file streams. – Michael Jul 14 '16 at 06:54
  • Yes, the limit is for files. This is an OS limit. Your OS doesn't know or care about stringstreams. – n. m. could be an AI Jul 14 '16 at 16:57

0 Answers0