作者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