fopen函数创建文件失败

陈心朔 2016-08-16 09:40:57
小白求教
当参数divide大于509时,就触发断言,fopen创建509.txt失败了
是因为在同一目录下不能创建太多文件吗?


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <vld.h>
#include <string.h>

#define NUM 1000000

//建立哈希表
typedef struct HashTable
{
int num;
int ntimes;
}HashTable;

//创建一个文件,写入100w个随机数
void CreateFile(const char *path)
{
assert(path != NULL);

FILE *fw = fopen(path,"wb");//以二进制写入方式打开(创建)文本文件
srand((unsigned int)time(NULL));
int tmp = 0;
for (int i = 0; i < NUM; i++)
{
tmp = rand();
fwrite(&tmp,sizeof(int),1,fw);//向文件中循环写入100w个随机数
}
fclose(fw);//关闭文件
}

//得到当前哈希文件中出现次数最多的数字
HashTable HashMax(FILE* fp,int divide)
{
int* arr = (int*)calloc(RAND_MAX/divide+1,sizeof(int));//计数器数组
int tmp = 0;
while (fread(&tmp,sizeof(int),1,fp))//当前数字所对应计数器加一
{
arr[tmp/divide]++;
}
HashTable ht = {0};
for (int i = 0; i < RAND_MAX/divide+1; i++)//得到当前文件中出现最多数字及次数
{
if (arr[i] > ht.ntimes)
{
ht.ntimes = arr[i];
ht.num = i*divide + tmp%divide;
}
}
free(arr);

return ht;
}


//若内存不足以创建足够大的计数器
HashTable Search_Million_plus(const char *path,int divide)
{
assert(path != NULL);
assert(divide > 0);

FILE** farr = (FILE**)malloc(divide*sizeof(FILE*));//定义一个文件指针数组
char tmp[200] = "";
for (int i = 0; i < divide; i++)//散列成divide份文件
{
sprintf(tmp,"D://%d.txt",i);
farr[i] = fopen(tmp/*D://i.txt*/,"w+b");

assert(farr[i] != NULL);
}

FILE* fr = fopen(path,"rb");
int num = 0;
while(fread(&num,sizeof(int),1,fr))//将从源文件读取到的数字写入对应的分列文件中
{
int t = num % divide;
fwrite(&num,sizeof(int),1,farr[t]);
}
for (int i = 0; i < divide; i++)//将哈希文件指针移至文件头
{
fseek(farr[i],0,SEEK_SET);
}

HashTable* pht = (HashTable*)malloc(divide*sizeof(HashTable));//建立一个结构体数组保存各哈希文件中出现最多的数
for (int i = 0; i < divide; i++)
{
pht[i] = HashMax(farr[i],divide);
}

HashTable ht = {0};
for (int i = 0; i < divide; i++)//找到出现次数最多的数
{
if (pht[i].ntimes > ht.ntimes)
{
ht.ntimes = pht[i].ntimes;
ht.num = pht[i].num;
}
}

for (int i = 0; i < divide; i++)//关闭文件,释放内存
{
fclose(farr[i]);
sprintf(tmp,"D://%d.txt",i);
remove(tmp);
}
free(farr);
free(pht);
fclose(fr);

return ht;
}

int main()
{
char* path = "D://input.txt";
//CreateFile(path);//创建文件

HashTable ht = Search_Million_plus(path,300);//divide < 509
printf("num = %d, ntimes = %d\n",ht.num,ht.ntimes);

return 0;

}

...全文
1002 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈心朔 2016-11-19
  • 打赏
  • 举报
回复
感谢楼上各位的回复,受教了
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
_setmaxstdio Sets a maximum for the number of simultaneously open files at the stdio level. int _setmaxstdio( int newmax ); Routine Required Header Compatibility _setmaxstdio <stdio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Returns newmax if successful; –1 otherwise. Parameter newmax New maximum for number of simultaneously open files at the stdio level Remarks The _setmaxstdio function changes the maximum value for the number of files which may be simultaneously open at the stdio level. C run-time I/O now supports many more open files on Win32 platforms than in previous versions. Up to 2,048 files may be open simultaneously at the lowio level (that is, opened and accessed by means of the _open, _read, _write, and so forth family of I/O functions). Up to 512 files may be open simultaneously at the stdio level (that is, opened and accessed by means of the fopen, fgetc, fputc, and so forth family of functions). The limit of 512 open files at the stdio level may be increased to a maximum of 2,048 by means of the _setmaxstdio function. Since stdio level functions, such as fopen, are built on top of the lowio functions, the maximum of 2,048 is a hard upper limit for the number of simultaneously open files accessed through the C run-time library. Note This upper limit may be beyond what is supported by a particular Win32 platform and configuration. Stream I/O Routines
赵老师果然强大
qq_35578084 2016-08-18
  • 打赏
  • 举报
回复
楼主,我建议你用CreateFile函数,WriteFile函数和CloseHandle函数来完成操作。 因为这些是正规的API函数。
赵4老师 2016-08-17
  • 打赏
  • 举报
回复
_setmaxstdio Sets a maximum for the number of simultaneously open files at the stdio level. int _setmaxstdio( int newmax ); Routine Required Header Compatibility _setmaxstdio <stdio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Returns newmax if successful; –1 otherwise. Parameter newmax New maximum for number of simultaneously open files at the stdio level Remarks The _setmaxstdio function changes the maximum value for the number of files which may be simultaneously open at the stdio level. C run-time I/O now supports many more open files on Win32 platforms than in previous versions. Up to 2,048 files may be open simultaneously at the lowio level (that is, opened and accessed by means of the _open, _read, _write, and so forth family of I/O functions). Up to 512 files may be open simultaneously at the stdio level (that is, opened and accessed by means of the fopen, fgetc, fputc, and so forth family of functions). The limit of 512 open files at the stdio level may be increased to a maximum of 2,048 by means of the _setmaxstdio function. Since stdio level functions, such as fopen, are built on top of the lowio functions, the maximum of 2,048 is a hard upper limit for the number of simultaneously open files accessed through the C run-time library. Note This upper limit may be beyond what is supported by a particular Win32 platform and configuration. Stream I/O Routines
陈心朔 2016-08-17
  • 打赏
  • 举报
回复
引用 2 楼 zjq9931 的回复:
因为你创建打开后不关闭,句柄资源被用完后就出错了。
新手还不知道句柄,刚才查了 谢谢~
陈心朔 2016-08-17
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
检查返回值 及 errno 判断 失败原因 具体看:http://en.cppreference.com/w/c/io/fopen
多谢!
  • 打赏
  • 举报
回复
因为你创建打开后不关闭,句柄资源被用完后就出错了。
paschen 2016-08-16
  • 打赏
  • 举报
回复
检查返回值 及 errno 判断 失败原因 具体看:http://en.cppreference.com/w/c/io/fopen

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧