![](/bink.gif) |
|
| ![](/bink.gif) |
|
![](icon/private_name/1.gif) |
8³â Àü |
ÆÄÀÏ Ã³¸®¸¦ ÇÏ´Ùº¸¸é ÆÄÀÏ °æ·Î¸¦ ÆÄÀϸí°ú ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î·Î ºÐ¸®ÇÏ¿©¾ß ÇÒ ¶§°¡ ÀÖ½À´Ï´Ù.
ÀÌ·² ¶§ Àüü °æ·Î¸¦ µå¶óÀ̺ê¿Í °æ·Î, ÆÄÀϸí, È®ÀåÀÚ·Î ³ª´©¾îÁÖ´Â _splitpath ÇÔ¼ö¸¦ ¾²°Å³ª °æ·Î¿¡¼
ÆÄÀϸíÀ» ¾ò¾îÁÖ´Â PathStripPath ÇÔ¼ö µîÀ» ¾µ ¼ö ÀÖÁö¸¸ Çì´õÆÄÀÏÀ̳ª ¶óÀ̺귯¸® ÆÄÀÏÀ» Ãß°¡Çؾß
Çؼ ´Ù¼Ò ¹«°Ì´Ù´Â ´ÜÁ¡ÀÌÀÖ½À´Ï´Ù.
±×·¡¼ °£´ÜÇÑ ¹®ÀÚ¿ 󸮷ΠÆÄÀϸí°ú ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î·Î ¹®ÀÚ¿À» ºÐ¸®ÇÏ´Â ÇÔ¼ö¸¦ ±¸¼ºÇÏ¿´½À´Ï´Ù.
1. ÆÄÀÏ¸í ¾ò±â
°æ·ÎÀÇ µÞºÎºÐ¿¡ ÀÖ´Â ÆÄÀϸíÀ» ã¾Æ¼ ÆÄÀϸíÀÇ ±æÀ̸¸Å µ¿ÀûÇÒ´çÇÑ ¹®ÀÚ¿ º¯¼ö¿¡ ÆÄÀϸíÀ»
ÀúÀåÇÕ´Ï´Ù. À̶§ GetFileNameFromPath ÇÔ¼ö ³»ºÎ¿¡¼ µ¿ÀûÇÒ´çÇÑ º¯¼ö´Â ÀÌ ÇÔ¼ö¸¦ È£ÃâÇÑ ÂÊ¿¡¼
ÇÒ´çÇØÁ¦ ÇØÁÖ¾î¾ß ÇÕ´Ï´Ù.
char *GetFileNameFromPath(const char *parm_path_name)
{
// ¹®ÀÚ¿ Æ÷ÀÎÅÍ¿¡ Áõ°¨¿¬»êÀÚ¸¦ ¼öÇàÇؾßÇϹǷΠÆ÷ÀÎÅÍ ¸Å°³ º¯¼ö¸¦ ´Ù¸¥ ¹®ÀÚ¿ Æ÷ÀÎÅÍ¿¡ º¹»çÇÑ´Ù.
const char *p_check_path = parm_path_name;
const char *p_pos = NULL;
// ¹®ÀÚ¿ÀÇ ³¡ÀÌ ¾Æ´Ñ °æ¿ì ¹Ýº¹¹®À» ¼öÇàÇÑ´Ù.
while(*p_check_path != '\0'){
// \\ ±âÈ£ À̰ųª / ±âÈ£ÀÎ °æ¿ì ÇØ´ç À§Ä¡¸¦ ÀúÀåÇÑ´Ù.
if(*p_check_path == '\\') p_pos = p_check_path;
else if(*p_check_path == '/') p_pos = p_check_path;
// ¹®ÀÚ¿ Æ÷ÀÎÅ͸¦ Áõ°¡½ÃŲ´Ù.
p_check_path++;
}
// \\ ±âÈ£ ´ÙÀ½ ¹®ÀÚ¸¦ °¡¸®Å°µµ·Ï Æ÷ÀÎÅ͸¦ Áõ°¡½ÃŲ´Ù.
p_pos++;
// p_pos ¿¡ ÀúÀåµÈ À§Ä¡ °ªÀÌ ÀÖ´Â °æ¿ì
if(p_pos != NULL){
// ÆÄÀϸíÀÇ ½ÃÀÛÀ» °¡¸®Å°´Â p_posºÎÅÍ ¹®ÀÚ¿ÀÇ ³¡±îÁöÀÇ ±æÀ̸¦ ±¸ÇÑ´Ù.
int length = strlen(p_pos);
// ¹®ÀÚ¿ ±æÀ̸¸Å µ¿ÀûÇÒ´çÇÑ´Ù.
char *p_file_name = new char[length + 1];
// ¹®ÀÚ¿À» º¹»çÇÑ´Ù.
strcpy(p_file_name, p_pos);
p_file_name[length] = 0;
return p_file_name;
}
return NULL;
}
void main()
{
char *p_path = "D:\\test\\temp\\WorkPing\\readme.txt";
// ÆÄÀϸíÀ» ¾ò´Â ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.
char *p_file_name= GetFileNameFromPath(p_path);
// ÆÄÀϸíÀ» Ãâ·ÂÇÑ´Ù.
printf("Àüü °æ·Î¸í : %s \nÆÄÀϸí : %s \n" p_path, p_file_name);
// ÆÄÀϸíÀ» ¸ðµÎ »ç¿ëÇÏ¸é µ¿ÀûÇÒ´çÀ» ÇØÁ¦ÇÑ´Ù.
delete[] p_file_name;
}
2. ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î¸í ¾ò±â
°æ·Î¿¡¼ ÆÄÀϸíÀº µÞºÎºÐ¿¡ Àֱ⶧¹®¿¡ ÆÄÀϸíÀÌ ½ÃÀÛÇÏ´Â À§Ä¡¿¡ 0 À» ÀúÀåÇÏ¿© ¹®ÀÚ¿À» ÀÚ¸¦ ¼öµµ
ÀÖ°í, º»·¡ÀÇ ¸Å°³ º¯¼ö¸¦ º¯°æÇÏ°í ½ÍÁö ¾ÊÀ¸¸é °æ·Î¸íÀ» µ¿ÀûÇÒ´çÇÏ¿© ¹ÝȯÇÒ ¼öµµ ÀÖ½À´Ï´Ù.
2.1 ¸Å°³ º¯¼ö¿¡ Á÷Á¢ °æ·Î¸í ÀúÀåÇϱâ
void GetPathExceptFileName(char *parm_path_name)
{
char *p_pos = NULL;
// ¹®ÀÚ¿ÀÇ ³¡ÀÌ ¾Æ´Ñ °æ¿ì ¹Ýº¹¹®À» ¼öÇàÇÑ´Ù.
while(*parm_path_name != '\0'){
// \\ ±âÈ£ À̰ųª / ±âÈ£ÀÎ °æ¿ì ÇØ´ç À§Ä¡¸¦ ÀúÀåÇÑ´Ù.
if(*parm_path_name == '\\') p_pos = parm_path_name;
else if(*parm_path_name == '/') p_pos = parm_path_name;
// ¹®ÀÚ¿ Æ÷ÀÎÅ͸¦ Áõ°¡½ÃŲ´Ù.
parm_path_name++;
}
// ÆÄÀÏ¸í ¾Õ¿¡ \\°¡ ÀÖ´Â À§Ä¡¿¡ \0À» ÀúÀåÇÑ´Ù.
if(p_pos != NULL) *p_pos = '\0';
}
void main()
{
// °æ·Î¸¦ ÀúÀåÇÑ´Ù. À̶§ p_path¿¡ ÀúÀåµÈ °æ·Î¸íÀº º¯°æµÉ °ÍÀ̹ǷΠchar *°¡ ¾Æ´Ñ
// char [] ·Î ¼±¾ðÇؾßÇÑ´Ù.
char p_path[] = "D:\\test\\temp\\WorkPing\\readme.txt";
// ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î¸íÀ» ¾ò´Â´Ù.
GetPathExceptFileName(p_path);
}
2.2 µ¿ÀûÇÒ´çÇÑ º¯¼ö¿¡ °æ·Î¸í ÀúÀåÇϱâ
char *GetPathExceptFileName(char *parm_path_name)
{
// ¹®ÀÚ¿ Æ÷ÀÎÅÍ¿¡ Áõ°¨¿¬»êÀÚ¸¦ ¼öÇàÇؾßÇϹǷΠÆ÷ÀÎÅÍ ¸Å°³ º¯¼ö¸¦ ´Ù¸¥ ¹®ÀÚ¿ Æ÷ÀÎÅÍ¿¡
// º¹»çÇÑ´Ù.
char *p_check_path = parm_path_name;
char *p_pos = NULL;
// ¹®ÀÚ¿ÀÇ ³¡ÀÌ ¾Æ´Ñ °æ¿ì ¹Ýº¹¹®À» ¼öÇàÇÑ´Ù.
while(*p_check_path != '\0'){
// \\ ±âÈ£ À̰ųª / ±âÈ£ÀÎ °æ¿ì ÇØ´ç À§Ä¡¸¦ ÀúÀåÇÑ´Ù.
if(*p_check_path == '\\') p_pos = (char *)p_check_path;
else if(*p_check_path == '/') p_pos = (char *)p_check_path;
// ¹®ÀÚ¿ Æ÷ÀÎÅ͸¦ Áõ°¡½ÃŲ´Ù.
p_check_path++;
}
// p_pos ¿¡ ÀúÀåµÈ À§Ä¡ °ªÀÌ ÀÖ´Â °æ¿ì
if(p_pos != NULL){
// \\±âÈ£°¡ ÀúÀåµÈ À§Ä¡ÀÇ ÁÖ¼Ò¿¡¼ °æ·ÎÀÇ ½ÃÀÛ ÁÖ¼Ò»©¼ °æ·ÎÀÇ ±æÀ̸¸Å µ¿ÀûÇÒ´çÇÑ´Ù.
char *p_path = new char[p_pos - parm_path_name + 1];
// ¿ø·¡ÀÇ °æ·Î¿¡¼ ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·ÎÀÇ ±æÀ̸¸ÅÀ» µ¿ÀûÇÒ´çÇÑ º¯¼ö¿¡ º¹»çÇÑ´Ù.
strncpy(p_path, parm_path_name, p_pos - parm_path_name);
// ¹®ÀÚ¿ÀÇ ¸¶Áö¸·¿¡ 0À» ÀúÀåÇÑ´Ù.
p_path[p_pos - parm_path_name] = 0;
return p_path;
}
return NULL;
}
void main()
{
char p_path[] = "D:\\test\\temp\\WorkPing\\readme.txt";
// ÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î¸íÀ» ¾ò´Â´Ù.
char *result_path = GetPathExceptFileName(p_path);
printf("Àüü °æ·Î¸í : %s \nÆÄÀϸíÀ» Á¦¿ÜÇÑ °æ·Î¸í : %s\n", p_path, result_path);
// µ¿ÀûÇÒ´çÀ» ÇØÁ¦ÇÑ´Ù.
delete[] result_path;
} |
|
̵̧ : 397 |
̵̧
¸ñ·Ï
|
|
| ![](/bink.gif) |
|