QT creater和vs2017文件路径问题

1. \\双反斜杠,传统写法,需转义

  • 在 C/C++ 字符串中,\ 具有特殊含义,例如:

    • \n 表示换行

    • \t 表示制表符

    • \" 表示双引号

  • 如果要表示一个真正的反斜杠,必须写成 \\,否则编译器会将其解释为转义字符。

2. /正斜杠,跨平台兼容

Qt 和现代 Windows API 都支持正斜杠/作为路径分隔符

3. 使用原始字符串(C++11 及以上)

R"(C:\Users\file.txt)"C++11 特性,避免转义

4. 双正斜杠(//

在文件路径中通常没有特殊含义,它会被当作两个单独的正斜杠处理。

文件系统(包括 Windows 和 Unix-like 系统)会自动将多个连续的斜杠合并为单个 /

有中文加u8

如何显示当前路径

win

cpp 复制代码
#include <direct.h>
#include <iostream>

int main() {
    char cwd[FILENAME_MAX];
    _getcwd(cwd, sizeof(cwd));
    std::cout << "Current working directory: " << cwd << std::endl;
    return 0;
}

linux

cpp 复制代码
#include <iostream>
#include <unistd.h>

int main() {
    char cwd[PATH_MAX];
    if (getcwd(cwd, sizeof(cwd)) != NULL) {
        std::cout << "Current working directory: " << cwd << std::endl;
    } else {
        std::cerr << "getcwd() error" << std::endl;
    }
    return 0;
}

跨平台(c++17)

cpp 复制代码
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main() {
    fs::path currentPath = fs::current_path();
    std::cout << "Current working directory: " << currentPath << std::endl;
    return 0;
}
相关推荐
m0_748252385 分钟前
Bootstrap 5 加载效果实现方法
c++
٩( 'ω' )و26022 分钟前
linux--库的制作与原理
linux
海盗123439 分钟前
VMware 中 CentOS 7 无法使用 yum 安装 wget 的完整解决方案
linux·运维·centos
人工智能AI技术1 小时前
GitHub Copilot 2026新功能实操:C++跨文件上下文感知开发,效率翻倍技巧
c++·人工智能
gtr20201 小时前
Ubuntu24.04 基于 EtherCAT 的 SVD60N 主站
linux·ethercat
weixin_462446232 小时前
ubuntu真机安装tljh jupyterhub支持跨域iframe
linux·运维·ubuntu
小码吃趴菜2 小时前
select/poll/epoll 核心区别
linux
大志若愚YYZ2 小时前
ROS2学习 C++中的this指针
c++·学习·算法
Ghost Face...2 小时前
深入解析网卡驱动开发与移植
linux·驱动开发
a41324472 小时前
在CentOS系统上挂载硬盘到ESXi虚拟机
linux·运维·centos