首页 > 编程语言 > 详细

c++之提取路径和文件名

时间:2021-06-29 15:03:12      阅读:16      评论:0      收藏:0      [点我收藏+]

代码

#include <iostream>
int main(int argc, char** argv)
{
  std::string file = "/home/download/test.png";
  int pos = file.find_last_of(‘/‘);
  std::string path(file.substr(0, pos));
  std::string name(file.substr(pos + 1));
  std::cout << "file path is: " << path << std::endl;
  std::cout << "file name is: " << name << std::endl;

  return 0;
}
file path is: /home/download
file name is: test.png

注: 使用std::string 的find_last_of做字符串分割

注: 使用std::string 的substr提取子字符串

参考

  • c++ primer 5

c++之提取路径和文件名

原文:https://www.cnblogs.com/chrislzy/p/14942289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!