免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7087 | 回复: 4
打印 上一主题 下一主题

[C++] 请教fopen能够同时打开的文件数上限 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-30 14:48 |只看该作者 |倒序浏览
我想知道在Linux、HP-Unix、Aix、Solaris等平台下,常见编译器实现的fopen能够同时打开的文件数上限。在相应的平台上,是否有系统设置或者编译器提供的函数可以被用来调整这个上限值。

比如,Windows下VC9实现的fopen默认能够最大打开512个文件,但可以通过_setmaxstdio函数设置其上限达到2048。对于其它平台及其上的编译器,由于没有相关平台可供测试,也没有相关的经验,故请教大家。

下面有一段测试代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>

  4. using namespace std;

  5. int main()
  6. {
  7.     int count = 0;
  8.    
  9.     for (;;)
  10.     {
  11.         char buf[256];
  12.         sprintf(buf, "logs\\log_%d.txt", ++count);
  13.         FILE *pFile = fopen(buf, "a+");
  14.         if (pFile == NULL)
  15.         {
  16.             cout << "Failed to create the file \"" << buf << "\"\n";
  17.             cout << "Error msg: " << strerror(errno) << endl;
  18.             break;
  19.         }
  20.         else
  21.         {
  22.             fprintf(pFile, "a line for test\n");
  23.             cout << "Created a file, No." << count << endl;
  24.         }
  25.     }
  26. }

复制代码

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2010-07-30 15:08 |只看该作者
ulimit -n

论坛徽章:
0
3 [报告]
发表于 2010-07-30 15:09 |只看该作者
回复 1# tyc611

  1. #include <sys/wait.h>
  2. #include <sys/types.h>
  3. #include <sys/param.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #ifdef OPEN_MAX
  10. static long openmax = OPEN_MAX;
  11. #else
  12. static long openmax = 0;
  13. #endif
  14. #define OPEN_MAX_GUESS 1024
  15. int main( int argc, char *argv[] )
  16. {
  17.   if( openmax == 0 ) {                // Run sysconf to determine the value
  18.     errno = 0;
  19.     if( ( openmax = sysconf( _SC_OPEN_MAX ) ) < 0 ) {
  20.       if( errno == 0 ) {
  21.         openmax = OPEN_MAX_GUESS;
  22.       } else
  23.         printf( "sysconf error" );
  24.     }
  25.   }
  26.   printf( "%ld\n", openmax );
  27.   return 0;
  28. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-07-30 22:17 |只看该作者
楼上所说的是可同时打开的文件描述符上限,并不是fopen()可同时打开的文件数

论坛徽章:
0
5 [报告]
发表于 2010-07-30 23:58 |只看该作者
在类UNIX系统下,2楼的方法可以。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP