Created
October 4, 2014 20:52
-
-
Save kazmura11/6453af48a3d03a179d5e to your computer and use it in GitHub Desktop.
How to handle string 1 NG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define MAX_PATH 260 | |
char *get_directory(const char *path) | |
{ | |
char dir[MAX_PATH+1]; | |
int i; | |
for (i = strlen(path) - 1; i >= 0; i--) | |
{ | |
if (path[i] == '/' || path[i] == '\\') | |
{ | |
strncpy(dir, path, i + 1); | |
dir[i + 1] = '\0'; | |
break; | |
} | |
} | |
return dir; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
char *dir; | |
char *path; | |
if (argc != 2) | |
{ | |
puts("Invalid argument!"); | |
return 0; | |
} | |
path = argv[1]; | |
printf("path: %s\n", path); | |
dir = get_directory(path); | |
printf("dir : %s\n", dir); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment