gpt4 book ai didi

c++ - 使用输入名称打开文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:30:19 25 4
gpt4 key购买 nike

我从与我一起工作的其他人那里获得了这段 C++ 代码,但我不确定为什么要添加“std::string()”。

std::ifstream File;
std::stringstream FileName;
FileName << Name; //Name being a string that has been passed as an input to the function.
// Eg."MyFile"
newFileName << ".txt"; //"MyFile.txt"

File.open(std::string(FileName.str()).c_str(), std::ios::in | std::ios::binary);

我的问题是,既然 str() 返回一个字符串,而 c_str() 获取一个字符串并将其转换为 c 字符串,为什么我们需要将它放在“string()”中?能不能这样写:

File.open((FileName.str()).c_str(), std::ios::in | std::ios::binary);

最佳答案

是的,可以这样写。

使用

std::string(FileName.str())

绝对没有意义。

关于c++ - 使用输入名称打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13180390/

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