如何在c ++中生成与操作系统无关的路径 [英] How to generate a OS independent path in c++

查看:128
本文介绍了如何在c ++中生成与操作系统无关的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标路径和一个文件名作为字符串,我想将它们与c ++连接。



有办法做到这一点,让程序/编译器在/和\之间选择窗口或unix系统?

解决方案如果你想在编译时执行,做类似

  #ifdef WIN32 
#define OS_SEP'\\'
#else
#define OS_SEP'/'
#endif

/',事情将在Windows上正常工作(除了解析字符串的只有使用'\'的旧程序)。它只是看起来很滑稽,如果向用户那样显示。


I have a destination path and a file name as strings and I want to concatenate them with c++.

Is there a way to do this and let the program/compiler choose between / and \ for windows or unix systems?

解决方案

If you wanted to do it at compile time you could certainly do something like

#ifdef WIN32
#define OS_SEP '\\'
#else
#define OS_SEP '/'
#endif

Or you could just use '/' and things will work just fine on windows (except for older programs that parse the string and only work with '\'). It only looks funny if displayed to the user that way.

这篇关于如何在c ++中生成与操作系统无关的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆