#include <stdio.h>
#include <limits.h>
#include <string.h>
#ifndef PATH_MAX
// This is woefully inadequate in normal situations,
// but then on "real" systems (i.e not here inb ideone.com) there would be
// an proper definition, or something similar that should be used instead
# define PATH_MAX 256
#endif
int main(void)
{
char full_path[] = "/some/path/to/a/file";
// Get the position of the last slash
char *last_slash
= strrchr(full_path
, '/');
// `last_slash` now points to the last slash, or is NULL if none is found
// That means `last_slash + 1` points to the first character in the file-name
// (or to the terminator if the path ends with a slash)
// Copy the file-name to another array
char file_name[PATH_MAX];
strcpy(file_name
, last_slash
+ 1);
printf("file_name = \"%s\"\n", file_name
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxsaW1pdHMuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2lmbmRlZiBQQVRIX01BWAovLyBUaGlzIGlzIHdvZWZ1bGx5IGluYWRlcXVhdGUgaW4gbm9ybWFsIHNpdHVhdGlvbnMsCi8vIGJ1dCB0aGVuIG9uICJyZWFsIiBzeXN0ZW1zIChpLmUgbm90IGhlcmUgaW5iIGlkZW9uZS5jb20pIHRoZXJlIHdvdWxkIGJlCi8vIGFuIHByb3BlciBkZWZpbml0aW9uLCBvciBzb21ldGhpbmcgc2ltaWxhciB0aGF0IHNob3VsZCBiZSB1c2VkIGluc3RlYWQKIyBkZWZpbmUgUEFUSF9NQVggMjU2CiNlbmRpZgoKaW50IG1haW4odm9pZCkKewoJY2hhciBmdWxsX3BhdGhbXSA9ICIvc29tZS9wYXRoL3RvL2EvZmlsZSI7CgoJLy8gR2V0IHRoZSBwb3NpdGlvbiBvZiB0aGUgbGFzdCBzbGFzaAoJY2hhciAqbGFzdF9zbGFzaCA9IHN0cnJjaHIoZnVsbF9wYXRoLCAnLycpOwoKCS8vIGBsYXN0X3NsYXNoYCBub3cgcG9pbnRzIHRvIHRoZSBsYXN0IHNsYXNoLCBvciBpcyBOVUxMIGlmIG5vbmUgaXMgZm91bmQKCS8vIFRoYXQgbWVhbn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