当前位置: 首页 > news >正文

C++文件路径处理2 - 路径拼接路径解析

  • 1. 关键词
  • 2. filesystem.h
  • 3. filepath.cpp
  • 6. 测试代码
  • 7. 运行结果
  • 8. 源码地址

1. 关键词

关键词:

C++ 文件路径处理 路径拼接 获取父目录的路径 获取文件名 获取拓展名 跨平台

应用场景:

  • 路径的拼接
  • 路径的解析

2. filesystem.h


#pragma once#include <string>
#include <iostream>
#include <cstdio>
#include "filetype.h"namespace cutl
{/*** @brief The class for file path operations.**/class filepath{public:/*** @brief Construct a new filepath object** @param path file path string*/filepath(const std::string &path);/*** @brief Construct a new filepath object by copy** @param other other filepath object*/filepath(const filepath &other);/*** @brief Assign operator, assign a new filepath object by copy** @param other other filepath object* @return filepath& the reference of the current filepath object*/filepath &operator=(const filepath &other);/*** @brief Destroy the filepath object**/~filepath() = default;public:/*** @brief Get the path separator of the current os platform.** @return the path separator*/static char separator();/*** @brief Get the string of the filepath.** @return the filepath*/std::string str() const;/*** @brief Join the current filepath with a new filename.** @param filename the filename to be joined* @return the new filepath object*/filepath join(const std::string &filename) const;/*** @brief Get the parent directory of the filepath.** @return parent directory path*/std::string dirname() const;/*** @brief Get the filename or directory name of the filepath.** @return filename or directory name*/std::string basename() const;/*** @brief Get the extension of the filepath.** @return extension with dot*/std::string extension() const;private:std::string filepath_;};/*** @brief Define the output stream operator for filepath object.** @param os the std::ostream object* @param fp the filepath object to be output* @return std::ostream& the reference of the std::ostream object after outputing the filepath object.*/std::ostream &operator<<(std::ostream &os, const filepath &fp);/*** @brief Create a filepath object from a string.** @param path file path string* @return filepath object*/filepath path(const std::string &path);} // namespace cutl

3. filepath.cpp


#include "filepath.h"
#include "inner/logger.h"
#include "inner/filesystem.h"
#include "strutil.h"
#include "sysutil.h"namespace cutl
{static constexpr char win_separator = '\\';static constexpr char unix_separator = '/';void fixpath(std::string &path){if (win_separator == filepath::separator()){for (size_t i = 0; i < path.size(); i++){if (path[i] == unix_separator){path[i] = win_separator;}}}else if (unix_separator == filepath::separator()){for (size_t i = 0; i < path.size(); i++){if (path[i] == win_separator){path[i] = unix_separator;}}}else{// do nothing}while (path.empty() || path.back() == filepath::separator()){path.pop_back();}}filepath::filepath(const std::string &path){filepath_ = path;fixpath(filepath_);}filepath::filepath(const filepath &other){filepath_ = other.filepath_;}filepath &filepath::operator=(const filepath &other){this->filepath_ = other.filepath_;return *this;}char filepath::separator(){
#if defined(_WIN32) || defined(__WIN32__)return win_separator;
#elsereturn unix_separator;
#endif}std::string filepath::str() const{return filepath_;}filepath filepath::join(const std::string &filename) const{std::string path = filepath_ + separator() + filename;return filepath(path);}std::string filepath::dirname() const{auto index = filepath_.find_last_of(separator());if (index == std::string::npos){return "";}return filepath_.substr(0, index);}std::string filepath::basename() const{auto index = filepath_.find_last_of(separator());// auto len = filepath_.length() - index - 1;if (index == std::string::npos){return filepath_;}return filepath_.substr(index + 1);}std::string filepath::extension() const{auto pos = filepath_.find_last_of('.');if (pos == std::string::npos){return "";}return filepath_.substr(pos);}std::ostream &operator<<(std::ostream &os, const filepath &fp){os << fp.str();return os;}filepath path(const std::string &path){return filepath(path);}
} // namespace cutl

6. 测试代码

#include "common.hpp"
#include "fileutil.h"void TestJoin()
{PrintSubTitle("TestJoin");auto path1 = cutl::path("/Users/spencer/workspace/common_util/src/usage_demo");auto path2 = path1.join("filepath.hpp");std::cout << "path1: " << path1 << std::endl;std::cout << "path2: " << path2 << std::endl;
}void TestDirnameBasename()
{PrintSubTitle("TestDirnameBasename");auto path1 = cutl::path("/Users/spencer/workspace/common_util/src/usage_demo/filepath.hpp");std::cout << "path1: " << path1 << std::endl;std::cout << "dirname: " << path1.dirname() << std::endl;std::cout << "basename: " << path1.basename() << std::endl;auto path2 = cutl::path("/Users/spencer/workspace/common_util/src/usage_demo");std::cout << "path2: " << path2 << std::endl;std::cout << "dirname: " << path2.dirname() << std::endl;std::cout << "basename: " << path2.basename() << std::endl;auto path3 = cutl::path("filepath.hpp");std::cout << "path3: " << path3 << std::endl;std::cout << "dirname: " << path3.dirname() << std::endl;std::cout << "basename: " << path3.basename() << std::endl;
}void TestExtenstion()
{PrintSubTitle("TestExtenstion");auto path1 = cutl::path("/Users/spencer/workspace/common_util/src/usage_demo/filepath.hpp");std::cout << "path1: " << path1 << ", extension: " << path1.extension() << std::endl;auto path2 = cutl::path("/Users/spencer/workspace/common_util/src/usage_demo/filepath");std::cout << "path2: " << path2 << ", extension: " << path2.extension() << std::endl;
}

7. 运行结果

----------------------------------------------TestJoin----------------------------------------------
path1: /Users/spencer/workspace/common_util/src/usage_demo
path2: /Users/spencer/workspace/common_util/src/usage_demo/filepath.hpp
----------------------------------------TestDirnameBasename-----------------------------------------
path1: /Users/spencer/workspace/common_util/src/usage_demo/filepath.hpp
dirname: /Users/spencer/workspace/common_util/src/usage_demo
basename: filepath.hpp
path2: /Users/spencer/workspace/common_util/src/usage_demo
dirname: /Users/spencer/workspace/common_util/src
basename: usage_demo
path3: filepath.hpp
dirname: 
basename: filepath.hpp
-------------------------------------------TestExtenstion-------------------------------------------
path1: /Users/spencer/workspace/common_util/src/usage_demo/filepath.hpp, extension: .hpp
path2: /Users/spencer/workspace/common_util/src/usage_demo/filepath, extension: 

8. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

本文由博客一文多发平台 OpenWrite 发布!

相关文章:

C++文件路径处理2 - 路径拼接路径解析

1. 关键词2. filesystem.h3. filepath.cpp6. 测试代码7. 运行结果8. 源码地址 1. 关键词 关键词&#xff1a; C 文件路径处理 路径拼接 获取父目录的路径 获取文件名 获取拓展名 跨平台 应用场景&#xff1a; 路径的拼接路径的解析 2. filesystem.h #pragma once#include…...

数据结构5---矩阵和广义表

一、矩阵的压缩存储 特殊矩阵:矩阵中很多值相同的元素并且它们的分布有一定的规律。 稀疏矩阵:矩阵中有很多零元素。压缩存储的基本思想是: (1)为多个值相同的元素只分配一个存储空间; (2)对零元素不分配存储空间。 1、特殊矩阵的压缩存储 &#xff08;1&#xff09;对称矩…...

jquery使用infinitescroll无线滚动+自定义翻页

jquery版本 jquery-1.8.3.js infinitescroll版本 2.0.0 如果infinitescroll版本最新的jquery版本也要用新的 接口用nodejs jquery.infinitescroll.js官网地址 前端代码《接口返回JSON数据》 <!DOCTYPE html> <html lang"en"> <head><meta cha…...

【漏洞复现】锐捷统一上网行为管理与审计系统——远程命令执行漏洞

声明&#xff1a;本文档或演示材料仅供教育和教学目的使用&#xff0c;任何个人或组织使用本文档中的信息进行非法活动&#xff0c;均与本文档的作者或发布者无关。 文章目录 漏洞描述漏洞复现测试工具 漏洞描述 锐捷统一上网行为管理与审计系统naborTable/static_convert.php…...

通义灵码上线 Visual Studio 插件市场啦!

通义灵码&#xff0c;是阿里云出品的一款基于通义大模型的智能编码辅助工具&#xff0c;提供行级/函数级实时续写、自然语言生成代码、单元测试生成、代码优化、注释生成、代码解释、研发智能问答、异常报错排查等能力&#xff0c;提供代码智能生成、研发智能问答能力。 通义灵…...

GESP 四级急救包(2):客观题真题集

客观题真题集 一、选择题1. 真题梳理2. 真题答案3. 重难点点播(1) 指针和地址(2) 时间复杂度 二、判断题1. 真题梳理2. 真题答案 一、选择题 1. 真题梳理 若函数声明为 void f(int &a, int b, const int &c)&#xff0c;且在主函数内已经声明了 x , y , z x,y,z x,y,…...

VERYCLOUD睿鸿股份确认参展2024年ChinaJoy BTOB商务洽谈馆,期待与你相聚

作为在全球数字娱乐领域兼具知名度与影响力的年度盛会&#xff0c;2024年第二十一届ChinaJoy将于7月26日至7月29日在上海新国际博览中心盛大召开&#xff0c;本届展会主题为&#xff1a;初心“游”在&#xff0c;精彩无限&#xff01;&#xff08;Stay True, Game On.&#xff…...

Java面试题:讨论Spring框架的核心组件,如IoC容器、AOP、事务管理等

Spring框架是一个功能强大且灵活的Java企业级应用开发框架&#xff0c;其核心组件包括以下几个主要部分&#xff1a; 1. IoC容器&#xff08;Inversion of Control Container&#xff09; IoC容器是Spring框架的核心部分&#xff0c;用于管理应用程序的依赖注入&#xff08;D…...

【方案】基于5G智慧工业园区解决方案(PPT原件)

5G智慧工业园区整体解决方案旨在通过集成5G通信技术、物联网、大数据和云计算等先进技术&#xff0c;实现园区的智能化、高效化和绿色化。 该方案首先构建高速、稳定的5G网络&#xff0c;确保园区内设备、人员与物流的实时连接和高效沟通。其次&#xff0c;通过工业物联网技术&…...

使用System.currentTimeMillis获取当前时间

使用System.currentTimeMillis获取当前时间 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天我们来探讨Java中如何使用System.currentTimeMillis()方法来获取…...

手机远程控制另一台手机的全新使用教程(安卓版)

看完这篇文章&#xff0c;你可以了解到安卓手机如何远程控制安卓手机&#xff0c;以及苹果手机如何远程控制安卓手机。 如果想要用安卓手机远程管控苹果手机&#xff0c;或者苹果手机远程管控另一台苹果手机&#xff0c;请点击查看视频《手机远程管控另一台手机的全新使用教程…...

商城积分系统的代码实现(上)-- 积分账户及收支记录

一、背景 上一系列文章&#xff0c;我们说了积分的数模设计及接口设计&#xff0c;接下里&#xff0c;我们将梳理一下具体的代码实现。 使用的语言的java&#xff0c;基本框架是spring-boot&#xff0c;持久化框架则是Jpa。 使用到的技术点有&#xff1a; 分布式锁&#xf…...

【C++进阶9】异常

一、C语言传统的处理错误的方式 终止程序&#xff0c;如assert 如发生内存错误&#xff0c;除0错误时就会终止程序返回错误码 需要程序员自己去查找对应的错误 z如系统的很多库的接口函数都是通 过把错误码放到errno中&#xff0c;表示错误 二、C异常概念 异常&#xff1a;函…...

RecyclerVIew->加速再减速的RecyclerVIew平滑对齐工具类SnapHelper

XML文件 ItemView的XML文件R.layout.shape_item_view <?xml version"1.0" encoding"utf-8"?> <FrameLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"100dp"android:layout_heig…...

突破SaaS产品运营困境:多渠道运营如何集中管理?

随着数字化时代的到来&#xff0c;SaaS&#xff08;软件即服务&#xff09;产品已成为企业日常运营不可或缺的工具。然而&#xff0c;在竞争激烈的市场环境下&#xff0c;SaaS产品运营越来越重视多渠道、多平台布局&#xff0c;以更广泛地触及潜在用户&#xff0c;然而&#xf…...

智能语音热水器:置入NRK3301离线语音识别ic 迈向智能家居新时代

一、热水器语音识别芯片开发背景 在科技的今天&#xff0c;人们对于生活品质的追求已不仅仅满足于基本的物质需求&#xff0c;更渴望通过智能技术让生活变得更加便捷、舒适。热水器作为家庭生活中不可或缺的一部分&#xff0c;其智能化转型势在必行。 在传统热水器使用中&#…...

Redis集群部署合集

目录 一. 原理简述 二. 集群配置​​​​​​​ 2.1 环境准备 2.2 编译安装一个redis 2.3 创建集群 2.4 写入数据测试 实验一&#xff1a; 实验二&#xff1a; 实验三&#xff1a; 实验四&#xff1a; 添加节点 自动分配槽位 提升节点为master&#xff1a; 实验…...

【HDFS】关于Hadoop的IPC.Client类的一些整理

org.apache.hadoop.ipc.Client 类是IPC服务的一个客户端。 IPC请求把一个Writable对象当做参数,返回一个Writable对象当做结果value。 一个IPC服务运行在某个端口上,并且由参数class和value class定义。 Router里的IPC.Client对象就两个 有这样一个类:ClientCache 看名字就…...

Swoole v6 能否让 PHP 再次伟大?

现状 传统的 PHP-FPM 也是多进程模型的的运行方式&#xff0c;但每个进程只能处理完当前请求&#xff0c;才能接收下一个请求。而且对于 PHP 脚本来说&#xff0c;只是接收请求和响应请求&#xff0c;并不参与网络通信。对数据库资源的操作&#xff0c;也是一次请求一次有效&am…...

C++ STL Iterator Adapter

1. std::back_insert_iterator 使用 // back_insert_iterator example #include <iostream> // std::cout #include <iterator> // std::back_insert_iterator #include <vector> // std::vector #include <algorithm> // std::copy…...

【Python】 -- 趣味代码 - 小恐龙游戏

文章目录 文章目录 00 小恐龙游戏程序设计框架代码结构和功能游戏流程总结01 小恐龙游戏程序设计02 百度网盘地址00 小恐龙游戏程序设计框架 这段代码是一个基于 Pygame 的简易跑酷游戏的完整实现,玩家控制一个角色(龙)躲避障碍物(仙人掌和乌鸦)。以下是代码的详细介绍:…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

进程地址空间(比特课总结)

一、进程地址空间 1. 环境变量 1 &#xff09;⽤户级环境变量与系统级环境变量 全局属性&#xff1a;环境变量具有全局属性&#xff0c;会被⼦进程继承。例如当bash启动⼦进程时&#xff0c;环 境变量会⾃动传递给⼦进程。 本地变量限制&#xff1a;本地变量只在当前进程(ba…...

【入坑系列】TiDB 强制索引在不同库下不生效问题

文章目录 背景SQL 优化情况线上SQL运行情况分析怀疑1:执行计划绑定问题?尝试:SHOW WARNINGS 查看警告探索 TiDB 的 USE_INDEX 写法Hint 不生效问题排查解决参考背景 项目中使用 TiDB 数据库,并对 SQL 进行优化了,添加了强制索引。 UAT 环境已经生效,但 PROD 环境强制索…...

解锁数据库简洁之道:FastAPI与SQLModel实战指南

在构建现代Web应用程序时&#xff0c;与数据库的交互无疑是核心环节。虽然传统的数据库操作方式&#xff08;如直接编写SQL语句与psycopg2交互&#xff09;赋予了我们精细的控制权&#xff0c;但在面对日益复杂的业务逻辑和快速迭代的需求时&#xff0c;这种方式的开发效率和可…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路

进入2025年以来&#xff0c;尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断&#xff0c;但全球市场热度依然高涨&#xff0c;入局者持续增加。 以国内市场为例&#xff0c;天眼查专业版数据显示&#xff0c;截至5月底&#xff0c;我国现存在业、存续状态的机器人相关企…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力

引言&#xff1a; 在人工智能快速发展的浪潮中&#xff0c;快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型&#xff08;LLM&#xff09;。该模型代表着该领域的重大突破&#xff0c;通过独特方式融合思考与非思考…...

微信小程序云开发平台MySQL的连接方式

注&#xff1a;微信小程序云开发平台指的是腾讯云开发 先给结论&#xff1a;微信小程序云开发平台的MySQL&#xff0c;无法通过获取数据库连接信息的方式进行连接&#xff0c;连接只能通过云开发的SDK连接&#xff0c;具体要参考官方文档&#xff1a; 为什么&#xff1f; 因为…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)

UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中&#xff0c;UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化&#xf…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...