gpt4 book ai didi

c++ - 如何摆脱打开文件中的 ""

转载 作者:行者123 更新时间:2023-11-28 00:13:30 27 4
gpt4 key购买 nike

我有这样的东西

/sth/sth/sth/SOMETHINGTHATISCHANGING/sth/sth.txt


string tab[5]={"nicefolder","betterfolder","bestofall","dog","readingthis"};
for(int i=0;i<5;i++)
{
fstream file;
file.open("/sth/sth/sth/"tab[i]"/sth/sth.txt", ios::in);
//shit to do
}

我知道这行不通,但只是想向您展示我需要实现的目标。这可能吗?

最佳答案

如果要进行字符串连接,可以使用+运算符

file.open("/sth/sth/sth/" + tab[i] + "/sth/sth.txt", ios::in);

顺便说一句,fstream 构造函数可以接受这些参数,因此您不需要进行单独的 open 调用

for(int i=0;i<5;i++)
{
fstream file{"/sth/sth/sth/" + tab[i] + "/sth/sth.txt", ios::in};
}

关于c++ - 如何摆脱打开文件中的 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841553/

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