windows 和 linux 的差异:C++ 中的反斜杠和正斜杠 [英] windows and linux discrepancy: backslash and forward slash in c++

查看:176
本文介绍了windows 和 linux 的差异:C++ 中的反斜杠和正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows中拥有

  std :: string graphdir = projDir +"graph \\";int mkdirsf = _mkdir(graphdir.c_str()); 

以上内容在Windows中效果很好.但是在Linux中,您知道使用正斜杠/.因此,上面将创建一个名为 graph \ 的文件夹.有没有一种通用的方法可以输入正确的文件夹而不必担心/ \ ?

解决方案

即使在Windows上,您也可以考虑使用正斜杠作为目录分隔符.大多数 Windows 库都能够将它们转换为反斜杠(它们实际上不进行转换,而是根据需要理解它们;其余的是实现细节)

否则,请注意 C++11(或 C++14)标准不知道文件夹"(您实际上是指 n3337 进行检查).C ++ 17具有 std ::文件系统.

也许您应该考虑其他一些库或框架: Boost Qt 都知道如何处理目录在常见的操作系统(Windows,Linux,MacOSX,Android)上.

一个更重要的问题是驱动器"字母.对于Windows(甚至某些MS-DOS) C:/FOO/BAR.TXT (或使用反斜杠, C:\ FOO \ BAR.TXT )和 D:/FOO/BAR.TXT 引用不同的文件.在Linux或MacOSX中没有真正的等效项.由于安装点更为笼统.

最后,从一个操作系统到另一个操作系统,文件层次结构约定(和文件系统)差异很大.对于 Linux,请参阅 hier(7) path_resolution(7).请注意, globbing 也是特定于操作系统的(并且发生的方式有所不同:在Unix系统中,它通常是通过 shells 完成的;在Windows上,它可以在每个应用程序中通过某些方式完成 crt0 类似于 glob(7).

顺便说一句,也许您可​​以考虑在Windows计算机上使用 WSL .在幸运的情况下,相同的可执行文件可以在Linux和Windows(在WSL下)运行,并且使您的工作更加轻松(在可用时).

花时间阅读有关操作系统 操作系统:三个简单的部分 教科书(可免费下载).

您会发现对阅读有关您的操作系统的更多信息很有用.对于Linux,请先阅读 ALP (或一些较新的书),然后阅读 mkdir(2) chdir(2) rmdir(2), nftw(3)等,等等.请注意,Linux上的文件只是 inode(7)以及关于硬链接),并且可以包含在几个中>目录(或无目录),请参见链接(2).AFAIU,这与Windows产生了很大的差异.

PS.我从未使用过Windows,也从未为此编写过代码.

In windows I have

std::string graphdir = projDir + "graph\\";
int mkdirsf=_mkdir(graphdir.c_str());

The above works quite well in windows. But in linux, you know forward slashed / are used. So the above will create a folder with name graph\. Is there an universal way to enter the correct folder without worrying about / or \?

解决方案

You might consider using forward slashes even on Windows as a directory separator. Most Windows libraries are able to convert them to backward slashes (they actually don't do a conversion, but understand them as wanted; the rest is an implementation detail)

Otherwise, notice that the C++11 (or C++14) standard don't know about "folders" (you actually mean directories; since folders are only a GUI artefact; read e.g. n3337 to check). C++17 has std::filesystem.

Maybe you should consider some other libraries or frameworks: Boost, POCO, Qt all know how to deal with directories on common OSes (Windows, Linux, MacOSX, Android).

A more significant concern is the "drive" letter. For Windows (and even some MS-DOS) C:/FOO/BAR.TXT (or, using backslashes, C:\FOO\BAR.TXT) and D:/FOO/BAR.TXT refer to different files. There is no real equivalent in Linux or MacOSX. Since mount points are more general.

At last, file hierarchy conventions (and file systems) vary widely from one OS to the other. For Linux, see hier(7) and path_resolution(7). Notice that globbing is also OS specific (and happens differently: in Unix systems, it is often done by shells; on Windows, it might be done, in every application, by some crt0 like thing of the runtime system). For Linux, see also glob(7).

BTW, perhaps you could consider using WSL on your Windows machine. In lucky cases, the same executable could run on Linux and on Windows (under WSL), and that makes your work easier (when it is usable).

Take time to read more about operating systems and file systems. I recommend the Operating System: Three Easy Pieces textbook (freely downloadable).

You could find useful to read more about your OS. For Linux, read ALP (or some newer book) then syscalls(2) and intro(3) etc... For Windows, learn the WinAPI (I don't know it), perhaps by starting here.

On Linux, the API relevant to directories include mkdir(2), chdir(2), rmdir(2), getcwd(2), stat(2), opendir(3) and closedir, readdir(3), nftw(3), etc, etc.... Be aware that a file is on Linux just an i-node (read inode(7) and about hard links) and can be in several directories (or none), see link(2). AFAIU, this makes a huge difference with Windows.

PS. I never used Windows, and never coded for it.

这篇关于windows 和 linux 的差异:C++ 中的反斜杠和正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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