文件是否存在
bool fileExists(const char *fileName)
{
ifstream infile(fileName);
return infile.good();
}
C/C++获取路径中的文件名
void getfilename(const char *filename, char *name)
{
int len = strlen(filename);
int i;
for (i = (len - 1); i >= 0; i--)
{
if ((filename[i] == '\\') || (filename[i] == '/'))
{
break;
}
}
strcpy(name, &filename[i + 1]);
return;
}
int main(){
char *buf = (char *)malloc(BUFSIZE);
memset(buf, 0, BUFSIZE);
getfilename(filename, buf);
}
#include <string>
#include <cstring>
# define SET_FILE(_ptr_, _filename_) do { \
_ptr_ = strrchr(_filename_, '/'); \
if (_ptr_ == NULL) \
_ptr_ = filename; \
else \
_ptr_++; \
} while (0)
int main()
{
const char *filename = "/home/oceanstar/acl_default_malloc";
const char *pname = nullptr;
SET_FILE(pname, filename);
printf("pname = %s", pname);
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201126181036171.png#pic_center)