Created
December 9, 2011 05:11
-
-
Save naoh16/1450277 to your computer and use it in GitHub Desktop.
basename
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 <stdlib.h> | |
#include <string.h> | |
void basename(const char* inFilename, char* outFilename, int outBuflen) | |
{ | |
char* p; | |
strncpy(outFilename, inFilename, outBuflen); | |
// 拡張子の削除. | |
if( (p = strrchr(outFilename, '.')) != NULL ) { | |
*p = '\0'; | |
} | |
if( (p = strrchr(outFilename, '/')) != NULL ) { | |
memmove(filename, p+1, p-filename); | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
char *input_filename = argv[1]; | |
char *p = 0; | |
char filename[2048]; | |
basename(input_filename, filename, 2048); | |
printf("%s -> %s\n", input_filename, filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment