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;
}
相关推荐
ybdesire10 小时前
在CentOS 7安装配置CodeQL与运行QL扫描
linux·运维·centos
wangnaisheng11 小时前
彩虹编码映射实现:C++与C#
c++·c#
waves浪游11 小时前
进程控制(下)
linux·运维·服务器·开发语言·c++
兵哥工控11 小时前
mfc两个线程的创建、启动、安全结束实例
c++·mfc·多线程·线程安全退出
Miketutu11 小时前
Dart基础学习
开发语言·windows
小龙报11 小时前
【算法通关指南:算法基础篇 】双指针专题:1.唯一的雪花 2.逛画展 3.字符串 4.丢手绢
c语言·数据结构·c++·人工智能·深度学习·算法·信息与通信
FirstFrost --sy11 小时前
Qt控件美化:LineEdit与CheckBox实战
开发语言·qt
Yusei_052311 小时前
Redis核心特性与应用全解析
开发语言·数据库·c++·redis·缓存
KYGALYX11 小时前
Win10/11系统下WSL2+Ubuntu的全流程安装
linux·运维·ubuntu
Yyyy48219 小时前
Ubuntu安装Jenkis
linux·运维·ubuntu