Skip to content

Instantly share code, notes, and snippets.

@naoh16
Created December 9, 2011 05:11
Show Gist options
  • Save naoh16/1450277 to your computer and use it in GitHub Desktop.
Save naoh16/1450277 to your computer and use it in GitHub Desktop.
basename
#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