forked from YanZhao/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileNameTools.cpp
114 lines (101 loc) · 2.36 KB
/
FileNameTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<string>
#include"FileNameTools.h"
using namespace std;
int GetDir(char *whole,char* dir)
{
char *index;
int len; /*储存长度*/
index=strrchr(whole,'/'); /*查找'/'在字符串whole中从后面开始的第一次出现的位置,并从此到末尾复制给index*/
if(NULL==index)
{
strcpy(dir,"");
return 0;
}
len=index-whole;
if(NULL == dir)
{
printf("Pointer is NULL @%s,%d",__FILE__,__LINE__);
return -1;
}
strncpy(dir,whole,len); /*将字符串whole中前len个字符拷贝到字符串dir中*/
dir[len]='\0'; /*加上字符串结束符*/
return len; /*返回长度*/
}
int GetName(char *whole,char* Name)
{
int len;
char *rname,*lname; /*name字符串的左边和右边*/
lname=strrchr(whole,'/'); //已经在GetDir中说明
rname=strrchr(whole,'.'); //拷贝拓展名
if(lname != NULL)
{
lname++; //多复制了'/',所以指针向右移一位
len=rname-lname; //得到不含拓展名的文件名的长度
}
else
{
len=rname-whole;
}
if(NULL == Name)
{
printf("Pointer is NULL @%s,%d",__FILE__,__LINE__);
return -1;
}
if(lname != NULL)
{
strncpy(Name,lname,len); //得到不含拓展名的文件名
}
else{
strncpy(Name,whole,len);
}
Name[len]='\0'; //加上字符串结束符
return len; //返回文件名长度
}
int GetExt(char *whole,char* Ext)
{
char *index;
int len;
index=strrchr(whole,'.'); //将拓展名(也就是文件名后缀)拷贝
if(NULL ==index)
{
strcpy(Ext,"");
return 0;
}
index++; //多复制了'.',故指针右移一位
len=strlen(whole)-strlen(index); //得到拓展名的长度
if(NULL == Ext)
{
printf("Pointer is NULL @%s,%d",__FILE__,__LINE__);
return -1;
}
strncpy(Ext,index,len); //拷贝拓展名
Ext[len]='\0'; //加上字符串结束符
return len; //返回拓展名长度
}
void InsertPartToFile(char *oldFileName,char* part,char* NewFileName)
{
char dir[250];
char name[250];
char ext[8];
GetDir(oldFileName,dir);
GetName(oldFileName,name);
GetExt(oldFileName,ext);
strcpy(NewFileName,dir);
strcat(NewFileName,name);
strcat(NewFileName,"-");
strcat(NewFileName,part);
strcat(NewFileName,".");
strcat(NewFileName,ext);
}
void TestFileNameTools(void)
{
char whole[]="c:/windows/system32/abc.dll";
char des[256];
GetDir(whole,des);
printf("%s\n",des);
GetName(whole,des);
printf("%s\n",des);
GetExt(whole,des);
printf("%s\n",des);
}