顺便说一句,也许您可以考虑在Windows计算机上使用 WSL .在幸运的情况下,相同的可执行文件可以在Linux和Windows(在WSL下)运行,并且使您的工作更加轻松(在可用时).花时间阅读有关操作系统和文件系统.我建议 操作系统:三个简单的部分 教科书(可免费下载).您会发现对阅读有关您的操作系统的更多信息很有用.对于Linux,请先阅读 ALP (或一些较新的书),然后阅读系统调用(2)和简介(3)等...对于Windows,请学习 WinAPI (我不知道),也许可以通过启动此处.在Linux上,与目录相关的API包括 mkdir(2), chdir(2), rmdir(2), getcwd(2), stat(2), opendir(3)和 closedir , readdir(3), nftw(3)等,等等.请注意,Linux上的文件只是 i节点(请阅读 inode(7)以及关于硬链接),并且可以包含在几个中>目录(或无目录),请参见链接(2).AFAIU,这与Windows产生了很大的差异. PS.我从未使用过Windows,也从未为此编写过代码.In windows I havestd::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++ 中的反斜杠和正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-06 04:50