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;
}
相关推荐
Sheffield1 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
不想写代码的星星1 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
Johny_Zhao2 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev2 天前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw
Felix_One2 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
樱木Plus3 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
0xDevNull3 天前
Linux切换JDK版本详细教程
linux
进击的丸子3 天前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
Johny_Zhao5 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw