C++ 如何将文件打印到不同的文件夹
在当前程序文件夹中生成文件。我不希望文件与正在编写它们的程序混合在一起C++ 如何将文件打印到不同的文件夹,c++,file,directory,C++,File,Directory,在当前程序文件夹中生成文件。我不希望文件与正在编写它们的程序混合在一起 std::string file = "Cell.txt"; myfile.open (file); 无所事事 std::string file = "Cell\\Cell.txt"; 没想到这会起作用,但还是试过了 std::string file = "\\Cell\\Cell.txt"; 我以前做过,在web上无法找到任何有帮助的方法您说您没有使用windows,但您创建了如下路径: std::string fi
std::string file = "Cell.txt";
myfile.open (file);
无所事事
std::string file = "Cell\\Cell.txt";
没想到这会起作用,但还是试过了
std::string file = "\\Cell\\Cell.txt";
我以前做过,在web上无法找到任何有帮助的方法您说您没有使用windows,但您创建了如下路径:
std::string file = "\\\\Cell\\\\Cell.txt";
这不是名为Cell
目录中名为Cell.txt
的文件,而是名为Cell\Cell.txt
的文件,因为反斜杠路径分隔符是windows ism,在其他操作系统下,它们是目录或文件名的一部分。改为使用正斜杠:Cell/Cell.txt
更好的是,使用新的C++文件系统库来构建与平台无关的方式,并完全避免这个问题。
std::string file = "Cell\\Cell.txt";
#包括
#包括
命名空间fs=std::实验::文件系统;
int main()
{
自动路径=fs::path(“Cell”)/fs::path(“Cell.txt”);
STD::在文件打开之前必须先查看文件路径。不使用Windows文件夹单元。运行程序,运行程序,删除程序,尝试不同的东西……考虑使用C++ 17中的新文件系统库(或标准版本的旧版本文件库)。要执行诸如构造路径和创建目录之类的操作。无法使#include工作,但更改为\并手动创建文件夹,worked@michaeleric您使用的是哪种编译器?名称和版本,如果可以的话。嗯,NetBeans不确定NetBeans 8.2修补程序2不知道在哪里可以找到它是哪种编译器using@michaeleric是p很有可能在下面使用g++,因此没有理由不起作用。我不熟悉netbeans的使用和配置,因此我无法建议您如何实际使其起作用。您可以尝试就此提出单独的问题。
std::string file = "Cell\\Cell.txt";
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
auto path = fs::path("Cell") / fs::path("Cell.txt");
std::cout << path.string() << std::endl;
}