Skip to content

Instantly share code, notes, and snippets.

@kazmura11
Created October 4, 2014 20:52
Show Gist options
  • Save kazmura11/6453af48a3d03a179d5e to your computer and use it in GitHub Desktop.
Save kazmura11/6453af48a3d03a179d5e to your computer and use it in GitHub Desktop.
How to handle string 1 NG
#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