69,373
社区成员
发帖
与我相关
我的任务
分享
char *CreateSearchPath(const char *perfix,const char *dirName)
{
char *newPath = NULL;
int perfixLen;
int suffixLen;
perfixLen = strlen(perfix);
suffixLen = strlen(dirName);
//新的字符串=perfix(如果其末尾有*,则去掉 + suffix(仅仅是文件名) + “\\*”)
if ('*' == perfix[perfixLen-1])//数组从0开始
{
perfixLen--;//去掉*字符
}
newPath = (char *)malloc(perfixLen+suffixLen+2);
#ifdef _DEBUG
printf("in function check:%d\t%d\t",perfixLen,suffixLen);
printf("in Funciton check%d\n",strlen(newPath));
#endif // _DEBUG
if (newPath)
{
memset(newPath, 0, perfixLen+suffixLen+2);
memcpy(newPath, perfix, perfixLen);
strcat(newPath, dirName);
strcat(newPath, newPath[perfixLen] == '\\' ? "*":"\\*");
return newPath;
}
else
{
return NULL;/*Probably out of RAM*/
}
}
void main()
{
char *a = "C:\\temp\\*";
char *b = "subDir";
/*3*/ char *c = CreateSearchPath(a,b);
printf("%s",c);
}
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char *CreateSearchPath(const char *perfix,const char *dirName)
{
char *newPath = NULL;
int perfixLen;
int suffixLen;
perfixLen = strlen(perfix);
suffixLen = strlen(dirName);
if ('*' == perfix[perfixLen-1])//数组从0开始
{
perfixLen--;//去掉*字符
}
newPath = (char *)malloc(sizeof(perfix[0]) * (perfixLen+suffixLen+3));
if(newPath)
{
strncpy(newPath,perfix,perfixLen);
strncpy(newPath+perfixLen,dirName,suffixLen);
strncpy(newPath+perfixLen+suffixLen,"\\*",2);
newPath[perfixLen+suffixLen+2]='\0';//这里加上
return newPath;
}
else
{
return NULL;/*Probably out of RAM*/
}
}
int main()
{
char *a = "C:\\temp\\*";
char *b = "subDir";
char *c = CreateSearchPath(a,b);
printf("%s",c);
return 0;
}