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;
}
相关推荐
普通网友19 分钟前
深入探讨Linux驱动开发:字符设备驱动开发与测试_linux 驱动开发设备号(2)
java·linux·驱动开发
martian66522 分钟前
深入解析C++驱动开发实战:优化高效稳定的驱动应用
开发语言·c++·驱动开发
hakuii25 分钟前
linux中的一些配置
linux·运维·服务器
TToolss32 分钟前
新手使用frp,3端全部Windows
windows
蓝天智能32 分钟前
QT实战:qrc资源动态加载
qt
时光の尘41 分钟前
嵌入式面试八股文(十九)·裸机开发与RTOS开发的区别
linux·stm32·单片机·iic·rtos·spi
FMRbpm1 小时前
用队列实现栈
数据结构·c++·新手入门
普通网友1 小时前
24年9月最新微软copilot国内Windows11强制开启使用教程方法
windows·ai·微软·copilot
瑾珮1 小时前
nmcli命令网络配置
linux·网络
qq_420443271 小时前
AMD显卡在windows中通过WSL安装使用stable diffusion(WebUI和ComfyUI)
linux·windows·ubuntu·stable diffusion·wsl