获取路径删除和串联字符
GetPath Remove and Concatenation Char
我将获得环境"APPDATA"并返回到从APPDATA\Roaming到APPDATA的上一个目录,然后我想与另一个目录APPDATA\Local\合并,最后将"\"替换为"\"。目前我遇到了问题。如何返回目录?
代码假设是
#include <string>
#include <iostream>
#include <algorithm>
char* path;
path= getenv("APPDATA"); <!--GetEnv APPDATA-->
??? <!-- Go back directory -->
strncpy(path,"Local\stuff,12); <!-- add front directory-->
<!-- Replace slash to double slash -->
std::string s = path;
std::replace(s.begin(),s.end(), '','');
您是否正在尝试删除字符串中的最后一个目录?
如果是这样,您可以使用 std::string::find_last_of( "\\" ) 查找最后一个斜杠,然后使用返回值创建子字符串。 以下示例将执行此操作。
std::string path = getenv("APPDATA"); //<!--GetEnv APPDATA-->
//??? <!-- Go back directory -->
std::size_t slashPosition = path.find_last_of( "\" );
// Remove slash at the end if found easier to handle if trailing slash is/not found)
path = path.substr( 0, slashPosition );
path += "\Local\stuff"; //<!-- add front directory-->
我删除了用双背替换单背的代码,因为它无法按编写的方式工作,我认为没有必要。 我还对路径变量使用了 std::string 来利用 std::string 中的方法。
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 在基于英语的系统上将 UTF-8 路径转换为宽字符会引发异常
- 使用连续字符及其路径查找矩阵中最长路径的长度
- 在Powershell中以编程方式转储路径时,重复字符被跳过
- 在 Windows 上,stat 和 GetFileAttributes 对于包含奇怪字符的路径失败
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 如何确定具有文件路径的字符串中是否有任何非 ASCII 字符
- 在 cpp 中验证路径:无法使用 ASCII 代码删除特殊字符
- C++ 路径文件存在限制为 260 个字符
- 获取路径删除和串联字符
- 如何将常量字符**路径分配为字符串
- 使用sf_open时,libsndfile将路径截断为512个字符
- BoostPython:在路径中加载带有unicode字符的Python模块
- 文件夹路径序列化的最大字符长度