Too many open files - C++ on Unix

Hi all,

My requirement is like this:
I have list of numbers, i need to get the data related to those numbers from database and write the information into a file .
I am using open function to write data related to one number. Close the file. Get the next number ,write data and close again.
I am using this logic as my file depends on some information that is being extracted for number. So every time, i prepare the file name (with that information: say xxx) and open the file in append mode. So if the extracted information (xxx) is already existing, this data will be appended to the existing file. Otherwise a new file will be created.

As per my understanding, as i am closing this file everytime, ideally speaking i will have only one open file at any time. But i am getting "Too many open files" errors.

Could some one please help me in resolving this issue.

Thanks in advance
Parvat

I can't read your code from here. Seeing the code would probably be helpful. Please use code tags when you post it, [ code ] stuff [ /code ] but without the extra spaces in the tags.

Here is the code that i am using:
I am giving the piece of code which is giving me this error. Thanks

ofstream feedin;

main() {
// for each account do the following
sprintf(file_name_var, "%s_%s_%s_%s_%s.dat",ProcessId, StartDate, StartTime, stmt_date, ssa_shrt_descr); // ssa_shrt_descr is derived from account.
feedin.open(file_name_var, ios::out | ios::app);
if (feedin == NULL){
    cout << "ERRS: Open Error - " <<  strerror(errno) << endl;
}
FormatFile(account, feedin);
feedin.close();
}