- 论坛徽章:
- 0
|
程序运行之后,内存不停往上增加,不知道问题出现在哪里,又没有用到new操作符,打开了文件也关了,找不到原因啊,麻烦朋友们帮忙诊断一下:
#include <string.h>
#include <stdio.h>
#include <windows.h>
#define FILE_ERROR -1
#define NOT_FOUND 0
#define SIZE 1024
//去空格
char * trim(char * ptr)
{
int start,end,i;
if (NULL != ptr)
{
for(start=0; isspace(ptr[start]) && ptr[start]!='\0'; start++)
;
for(end=strlen(ptr)-1; isspace(ptr[end]) && end>=0; end--)
;
//传递进来的字符串全部是空格,返回""
if (end < 0)
{
ptr[0] = '\0';
}
else //存在其他字符
{
for(i=start; i<=end; i++)
ptr[i-start]=ptr[i];
ptr[end-start+1]='\0';
}
return (ptr);
}
else
return NULL;
}
int findString(char * fileName,
char * findString,
char * returnMsg,
bool careCase)
{
int returnValue = 0;
int findFlag = 0;
FILE * file;
if (NULL == (file=fopen(fileName, "r")))
{
printf("函数getInfo()中文件 %s 打开失败或不存在!\n", fileName);
SetLastError(FILE_ERROR);
return 0;
}
char buf[SIZE];
char * value;
while (NULL != fgets(buf, SIZE, file))
{
returnValue++;
trim(buf);
if (!strcmp(buf, ""))
continue;
value = strstr(buf, findString);
if (value != NULL)
{
strcpy(returnMsg, buf);
findFlag = 1;
break;
}
}
fclose(file);
if (!findFlag)
{
SetLastError(NOT_FOUND);
return 0;
}
return returnValue;
}
void fun()
{
int i;
char buf[1024];
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
char file[256];
while (1)
{
//printf("while\n");
hFind = FindFirstFile(".\\config\\*.xml", &FindFileData);
LOOP:
sprintf(file, ".\\config\\%s", FindFileData.cFileName);
//strcpy(file, ".\\config\\");
//strcat(file, FindFileData.cFileName);
i = findString(file, "sdfcpp", buf, true);
if (FindNextFile(hFind, &FindFileData) != 0)
goto LOOP;
else
continue;
}
}
void main()
{
fun();
} |
[ 本帖最后由 hinku 于 2008-7-15 21:09 编辑 ] |
|