gpt4 book ai didi

c++ - C++中使用包含引号的字符串指定ofstream的文件路径

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:04 25 4
gpt4 key购买 nike

我正在尝试传递一个字符串作为 ofstream 的路径。我正在使用 .c_str() 函数,但程序在不生成文件的情况下运行。

ifstream path_stream ("config.txt");
path_stream >> path;
path_stream.close();
ofstream datum (path.c_str());

config.txt的内容是

"test.txt"

如果我将其直接提供给 ofstream,程序会创建文件 test.txt
使用字符串,它只运行程序而没有任何输出。

最佳答案

问题是您将 config.txt 文件中的文件路径用引号引起来了。

If I give that directly to ofstream, the program creates the file test.txt.

我想你已经试过了

ofstream datum ("test.txt");

这与

有很大的不同
ofstream datum ("\"test.txt\"");
// ^^ ^^

因为这相当于

ofstream datum (path.c_str());

从文件中读取path 之后。

因此只需从您的 config.txt 文件中删除引号即可。

关于c++ - C++中使用包含引号的字符串指定ofstream的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com