作者asd550 ()
看板EE_DSnP
标题[问题] 请问有关ifstream的问题
时间Fri May 29 18:16:05 2009
想要开档!
但是档案名称是变动的!
所以用一个string去接他。
ifstream infile;
vector<string> options;
CmdExec::lexOptions(option,options);
int number = options[0].length();
char* name = new char[number];
options[0].copy(name,number,0);
const char* file =name;
cout << endl;
infile.open(file,ifstream::in);
if (!infile.is_open())
cout << "This file can't be opened" << endl;
else {
while(infile.good())
cout << (char)infile.get();
infile.close();
}
return....
这样compile的结果
每次执行第一次的时候,档案都不能开启,但是执行第二次就可以!
不知道为什麽?
cir> CIRR C17.cir
This file can't be opened
cir> CIRR C17.cir
.cir C17
.input 1GAT 2GAT 3GAT 6GAT 7GAT
.output 22GAT 23GAT
and 11GAT 3GAT 6GAT
and 10GAT 1GAT 3GAT
inv 11GAT_B 11GAT
and 19GAT 11GAT_B 7GAT
and 16GAT 2GAT 11GAT_B
or 23GAT 16GAT 19GAT
or 22GAT 10GAT 16GAT
.end
这是执行结果!
有请强者!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.245.66
※ 编辑: asd550 来自: 140.112.245.66 (05/29 18:17)
1F:推 timrau:char* name并没有以'\0'结尾喔 05/29 18:38
2F:→ timrau:你可以试试infile.open(options[0].c_str(), ios::in); 05/29 18:38
3F:→ asd550:多谢多谢 05/29 18:46
4F:→ asd550:太感谢了! 05/29 18:49