Thread: Too many files open?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Too many files open?

    I keep getting an error saying that I have too many files open when trying to open a file. I have a function that gets called a lot of times and I'm trying to trace some stuff going on in that function. So when I enter the function, I open a file to append to, write some stuff to it, and then close it. After a while, when I try to open the file to append to, I get a seg fault because too many files are open. My code is basically:

    Code:
    FILE *fp = fopen(outFile, "a+");
    if(fp!=NULL){
     fprintf(fp,"stuff\n");
     fclose(fp);
    }else{
     printf("Error opening file\n");
    }
    is there some other way I'm supposed to close the file?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Welcome.

    There's nothing wrong with what you posted. If you provide us with a complete source sample that reproduces your issue, and the compiler/environment in which you're running - we should be able to help.

    gg

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What your error message is telling you is there are a lot more fopen calls than fclose calls.

    So whilst your approximation of the problem is impeccable, the reality of your situation is different.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Open GL header files
    By Rune Hunter in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2005, 01:10 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM