I have written code that opens a series of files, without closing any of them (for a while), and stores the handles in an array of fsteam* pointers. I've compiled and run the code using Microsoft Visual C++ .NET 2003 under Windows XP. The code is a console application that runs in a CMD window. The code works fine with an array of, say, 360 files. When I try to use an array of 720 files, it fails to open the the 510'th file in the series (the file is numbered 509).
I have verified that it is not the file itself by substituting a known good file (number 508). I have verified that it relates to number of open files by closing each file after it is open. Then 509 opens correctly. I have verified that it is a certain number of files by opening 10 files in advance of the loop that fails, and the file number on failure is reduced by 10 to 499.
So, it seems to be either a Windows or a VC ++ limit on the number of files that can be kept open at any one time. This number 510 / 509 is independent of how many files I have open for other purposes in Windows. It seems to be only a function of how many files are opened in the CMD window.
So, my question is, does anyone know the cause of this problem and how to fix it?
Relevant sections of the code are reproduced below.
Thanks in advance.
----------------------
The code
----------------------
//.... Allocate an array of pointers to fstream objects
rawfile = (fstream **) malloc(inpt.rotations * sizeof(fstream*));
for(c=0;c<inpt.rotations;c++)
{
//.... Determine the name of the file (char* s1)
sprintf(s1, "%s%0*i.raw", inpt.output_prefix, inpt.num_digits, c);
//.... Construct an fstream object and store its pointer in the array
rawfile[c] = (fstream*) new fstream();
if (rawfile[c] == NULL)
{
cout << "ERROR: could not allocate fstream object in sin_create,"
<< " rawfile["<<c<<"]" << endl;
return 0;
}
//.... Open the file for the fstream object
rawfile[c]->open(s1, ios::in | ios::binary);
if (rawfile[c]->fail())
{
cout << "ERROR: failed to open file in sin_create, " << c << " = "
<< s1 << endl;
return 0;
}
}
//.... Other code that operates on the set of open files
-------------------------
It is failing on the last ERROR check.
I have verified that it is not the file itself by substituting a known good file (number 508). I have verified that it relates to number of open files by closing each file after it is open. Then 509 opens correctly. I have verified that it is a certain number of files by opening 10 files in advance of the loop that fails, and the file number on failure is reduced by 10 to 499.
So, it seems to be either a Windows or a VC ++ limit on the number of files that can be kept open at any one time. This number 510 / 509 is independent of how many files I have open for other purposes in Windows. It seems to be only a function of how many files are opened in the CMD window.
So, my question is, does anyone know the cause of this problem and how to fix it?
Relevant sections of the code are reproduced below.
Thanks in advance.
----------------------
The code
----------------------
//.... Allocate an array of pointers to fstream objects
rawfile = (fstream **) malloc(inpt.rotations * sizeof(fstream*));
for(c=0;c<inpt.rotations;c++)
{
//.... Determine the name of the file (char* s1)
sprintf(s1, "%s%0*i.raw", inpt.output_prefix, inpt.num_digits, c);
//.... Construct an fstream object and store its pointer in the array
rawfile[c] = (fstream*) new fstream();
if (rawfile[c] == NULL)
{
cout << "ERROR: could not allocate fstream object in sin_create,"
<< " rawfile["<<c<<"]" << endl;
return 0;
}
//.... Open the file for the fstream object
rawfile[c]->open(s1, ios::in | ios::binary);
if (rawfile[c]->fail())
{
cout << "ERROR: failed to open file in sin_create, " << c << " = "
<< s1 << endl;
return 0;
}
}
//.... Other code that operates on the set of open files
-------------------------
It is failing on the last ERROR check.