求教c++问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 11:54:05
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
int main(){
int a,b,yonghuming;
cout<<"请输入学生本人信息:"<<endl;
string temp;
string yonghuming;
cin>>yonghuming;
cout<<"请输入学号:"<<endl;
cin>>a;
cout<<"请输入学号:"<<endl;
cin>>b;
temp="d:\\";
temp+=yonghuming;
temp+=".txt";
ofstream ofile(temp.c_str());
ofile<<"该学生的学号是:"<<a<<endl;
ofile<<"该学生的密码是:"<<b<<endl;
return 0;
}

temp="d:\\";
temp+=yonghuming;
temp+=".txt";
ofstream ofile(temp.c_str());
这个请详细解释一下

temp="d:\\";
temp+=yonghuming;
temp+=".txt";
ofstream ofile(temp.c_str());

打开一个txt文件
由于yonghuming由用户指定
所以需要这样来用
temp是一个std::string对象
而ofstream ofile()需要一个char *的参数
所以使用std::string的方法c_str()将其转换为char*

temp是创建文件的路径就是d:\yonghuming.txt
在d盘创建yonghuming.txt文件。文件内容是:
该学生的学号是:a
该学生的密码是:b