C:打开太多文件

这段代码打开一个目录,并且对于目录中的每个文件,它循环遍历文件中的每一行数据,然后parsing它进行一些计算,并将结果数据输出一个文件中。

问题是我最多只能输出大约1021个文件输出所有的数据后,我closures所有的fopens ,所以我不知道我做错了什么。

不应该fclose()closures打开的文件,因此这不会发生?

int main(int argc,char *argv[]) { //sample data values double lat; double lon; double convergence; double pt_scale; int zone = 54; double major_axis = 6378137.0000; double flattening = (1/298.2572); double zoneWidth = 6; double centMeridian = -177; double falseEast = FALSE_EASTING; double falsenorth = FALSE_norTHING; double scale = SCALE_FACTOR; int max_size = 128; int current_size = max_size; char *pathStr = malloc(max_size); char *outPathStr = malloc(max_size); char coData[100]; //max length of line; long firstTerm,secondTerm; //terms we will split the line into,lat,lon,elevation. int counter = 0; //pos counter int d = EOF; //end of file ASCII char strIn[200]; char* elevation; char strOut[200]; char dirOut[200]; //sprintf must use a actual defined buffer otherwise there will be a buffer overflow. char* cchr; int j; _setmaxstdio(2048); printf("Please enter the path of the files: n"); getUserInput(pathStr,current_size,max_size); printf("Please enter the output path of the files: n"); getUserInput(outPathStr,max_size); //loop through each file in the directory. Open the file,convert,then close it. //we will use dirent.h as it is cross platform so we wont have to worry about sharing issues DIR *dir; //new directory struct dirent *ent; dir = opendir(pathStr); //allcate it a path if(opendir(pathStr) == NULL) { printf("Error: %d (%s)n",errno,strerror(errno));} int k; if(dir != NULL) { while((ent = readdir(dir)) != NULL) //loop through each file in the directory. { //open the file and loop through each line converting it then outputing it into a new file if((!strcmp(ent->d_name,"..") || !strcmp(ent->d_name,".")) == 1) { //dont want these directories continue; } else { sprintf(strIn,"%s%s",pathStr,ent->d_name); //get the file n FILE *fp = fopen(strIn,"r"); if(fopen(strIn,"r") == NULL) //for inputting file { printf("Error: %d (%s)n",strerror(errno)); getchar(); break; } sprintf(dirOut,"%s%d%s",outPathStr,counter,".geo"); printf("%s n",dirOut); FILE *fp2 = fopen(dirOut,"w"); //for outputting file if(fopen(dirOut,"w") == NULL) { printf("Error: %d (%s)n",strerror(errno)); getchar(); break; } while(fgets(coData,100,fp) != NULL)//loop through line by line,allocate into 2 doubles and a string,pass the two coordinates and convert { //extract terms from coData char * pch; //pointer to array pos char * pend; pch = strtok(coData," "); j = 0; while(j <= 2) //We only want to split the first three parameters. { //convert char array to double for co-oridinate conversion if(j == 0) { firstTerm = atof(pch); //latitude; j++; continue; } if(j == 1) { pch = strtok(NULL," "); secondTerm = atof(pch); //longitude j++; continue; } if(j == 2) { pch = strtok(NULL," "); elevation = pch; //elevation doesnt need to be converted because it isnt used in the coordinate conversion. break; } } grid2spheroid(&lat,&lon,&convergence,&pt_scale,firstTerm,secondTerm,zone,major_axis,flattening,zoneWidth,centMeridian,falseEast,falsenorth,scale); sprintf(strOut,"%f %f %s",elevation); //printf("%d %d",lon); fputs(strOut,fp2); } //end of while fclose(fp2); fclose(fp); counter++; } } closedir(dir); } free(pathStr); //finished using the path string so we can finish the free(outPathStr); getchar(); return 0; } void getUserInput(char *pathStr,int current_size,int max_size) { unsigned int i = 0; if(pathStr != NULL) { int c = EOF; //get the user input and reallocate the memory size if the input it too large. while((c = getchar()) != 'n' && c != EOF) //WHILE NOT END OF FILE OR NEW LINE (USER pressed ENTER) { pathStr[i++] = (char)c; if(i == current_size) { current_size = i+max_size; pathStr = realloc(pathStr,current_size); } } } }

在python中使用subprocess运行utf-8编码的windowsbatch file

如何在Windows服务程序中捕获SERVICE_CONTROL_SHUTDOWN代码

使用MapViewOfFile,指针最终走出内存空间

如何使用c + +得到Windows DNS后缀search列表

如何在pypi上为多个版本的Python分发预编译的Windows扩展模块?

如何在我们的应用程序之外控制鼠标指针

如何从Stdin Stream中同时处理两个input,其中一个来自C中的数字键盘

如何在Windows 7桌面上接受传入的蓝牙连接(使用c ++或c#程序)

在Windows上获取Python的最后更改时间

Winforms通知图标在系统托盘中重复

你没有关闭所有的文件;-)

FILE *fp = fopen(strIn,"r") == NULL) //for inputting file

同样适用于您的输出

我觉得你的意思更像是:

FILE *fp = fopen(strIn,"r"); if(fp == NULL) //for inputting file { // error handling.

不,不! 你打开每个文件两次 (只关闭一次)!

/* Bad! */ dir = opendir(pathStr); //allcate it a path if(opendir(pathStr) == NULL) { printf("Error: %d (%s)n",strerror(errno));} int k; /* Correct */ dir = opendir(pathStr); //allocate it a path if(!dir) { printf("Error: %d (%s)n",strerror(errno)); return; }

你也用fopen()做同样的事情。 在这两个地方:)

只要检查指针 ; 不要再打“fopen()”; 不要再次调用“opendir()”!

另外:请不要把代码放在与你的左大括号相同的行上。 好?

dir = opendir(pathStr); //allcate it a path if(opendir(pathStr) == NULL) (...) FILE *fp2 = fopen(dirOut,"w") == NULL) (...) FILE *fp = fopen(strIn,"r") == NULL) //for inputting file

在这里你打开文件两次,但只存储指针一次。 将这些更改为:

FILE *fp = fopen(strIn,"r"); if(fp == NULL) //for inputting file

一个以同样的方式。

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....