Thread: max num of simultaneously opened files in a c++ program.

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    max num of simultaneously opened files in a c++ program.

    Is there a way to find this out?

    I looked up on google, but the only related thing i found is _setmaxstdio which is a C function. I suppose it also applies in C++?

    Also, what does this depend on? On the OS or the compiler?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd say that this depends on the OS, and any way to manipulate this from your program would also be OS specific.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's a constant called FOPEN_MAX in limits.h that is a minimum of 20 on most systems, I believe. Of course that's for fopen() and doesn't apply to C++ streams but it might give you some idea at least of the actual number.

    Why do you want to know this? What are you trying to do?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Tool View Post
    Is there a way to find this out?

    I looked up on google, but the only related thing i found is _setmaxstdio which is a C function. I suppose it also applies in C++?

    Also, what does this depend on? On the OS or the compiler?
    There's no standard way to get that information (_setmaxstdio/_getmaxstdio are MS extensions). Anyway, the limit is going to depend on the OS, and as far as standard functions go (ie: fopen, et al), their limits are going to depend on the particular compiler used (although most implementations probably implement the OS default value).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I'm surprised this hasn't been mentioned before:
    In Windows, I have no idea how to get the maximum number of files. In Linux you can use getrlimit with the constant RLIMIT_NOFILE. This limit can be configured and can be different on a per program basis. Try running "ulimit -n 1" and then run something like "cat /etc/passwd". Here you set the maximum number of files to 1, which isn't enough and it causes an error. If you have permissions you can call "setrlimit" to change this limit (but there's a hard limit, an actual maximum for this).
    All in all, read the setrlimit man pages if you use Linux.

    Technically, this limit may not apply to C++ files. But I can't imagine any implementation (especially not of Linux) that does not have one file descriptor per open file.
    Keep in mind that stdin, stdout and stderr are file descriptors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-23-2009, 09:47 PM
  2. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  3. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  4. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  5. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM