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;
}
相关推荐
ShineWinsu2 小时前
对于C++:auto_ptr、unique_ptr、shared_ptr的模拟实现
c++·面试·笔试·智能指针·unique_ptr·shared_ptr·auto_ptr
张洛闻Eren3 小时前
MySQL 维护稳定系统【MySQL第二课】
linux·数据库·mysql·云原生
ltl3 小时前
Linux 异步 I/O:epoll 与 io_uring 对比
linux
丰锋ff3 小时前
3.1 Qt事件之事件处理器
开发语言·qt
ltl3 小时前
压缩算法工程实践:吞吐、比率与 CPU 权衡
linux
fiveym4 小时前
01 - iPXE + Clonezilla 网络装机原理解析
linux·运维·服务器·网络
XLYcmy4 小时前
京东 算法实习一面 下+手撕
c++·python·llm·概率论·数据处理·训练·codebert
反转180度5 小时前
Qt 6 + C++17 实现 SFTP 批量部署:工业设备运维工具实战解析
运维·c++·qt
-今昭-6 小时前
Ansible
linux·运维·ansible
sudebao点com6 小时前
一次“Google 能打开,FlexTV 打不开”的 DNS 故障排查:从 curl 超时到完整证据链
windows·macos·https·cdn·dns·nslookup·网络排错