将一个字符串赋值给一个char型数组出了问题,高手请进!
大家好,我的程序很简单的,请大家看看问题在哪里,谢谢。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void read(ifstream &T)
{
char ch;
while(!T.eof()){
T.get(ch);
cout < < ch;
}
cout < < endl < < "-------- " < < endl;
}
int main(){
cout < < "please choose the file to create the virtual world:1 or 2? " < <endl;
int x;
char filename[10];
cin> > x;
while(cin){
if(x==1){
filename[]= "1.txt ";break;
}
else if(x==2){
filename[]= "2.txt ";break;
}
else{
cout < < "the input is error!please input again! " < <endl;
cin> > x;
}
}
ifstream fin;
fin.open(filename,ios::in);
if(!fin)
{
cout < < "The file " < <filename < < "cannot be opened! " < <endl;
exit(1);
}
read(fin);
return 0;
}
[解决办法]
filename[]= "1.txt ";break;
改为
strcpy(filename, "1.txt ");break;
将一个字符串赋值给一个char型数组出了有关问题,
将一个字符串赋值给一个char型数组出了问题,高手请进!大家好,我的程序很简单的,请大家看看问题在哪里,谢谢
