输入盘符,文件夹名,文件名,输出该文件所在路径,以“/”连接
string可以直接用+拼接啊
如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pf,path,filename;
string all;
cout << "请输入盘符:";
cin >> pf;
cout << "请输入文件夹名:";
cin >> path;
cout << "请输入文件名:";
cin >> filename;
all = pf + ":/"+path+"/"+filename;//如果盘符后面需要两个//,那么就改成 all = pf + "://"+path+"/"+filename;
cout << all<<endl;
return 0;
}
如果路径中有\要替换成/,代码如下:
代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pf,path,filename;
string all;
string t="";
int i=0;
cout << "请输入盘符:";
cin >> pf;
cout << "请输入文件夹名:";
cin >> path;
//将path中的\\替换成/
while(i<path.length())
{
if(path.at(i)=='\\')
t+='/';
else
t+=path.at(i);
i++;
}
cout << "请输入文件名:";
cin >> filename;
all = pf + ":/"+t+"/"+filename;//如果盘符后面需要两个//,那么就改成 all = pf + "://"+path+"/"+filename;
cout << all<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string result;
string inputStr;
cout << "please input disk: ";
cin >> inputStr;
result = inputStr;
result += ":";
while (true){
int type = 0;
cout << "type:\n\tfolder: 0\n\tfile name: 1\n";
cout << "input type code = ";
cin >> type;
if (type == 0){
cout << "input folder = ";
cin >> inputStr;
result += "/";
result += inputStr;
}
else{
cout << "input file name = ";
cin >> inputStr;
result += "/";
result += inputStr;
break;
}
}
cout << "file path = " << result << endl;
system("pause");
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!