[問題] fstream reading file error
開發平台(Platform): Linux
問題(Question):
在測試一個很簡單的讀檔案功能,可是一直失敗
環境是在linux上用g++ compile
要測試取的檔案叫做text,位在cpp與執行檔同一個資料夾下
complile好的執行檔叫做a.out
>> ./a.out text
這樣的話會讀取失敗
但是如果我把code內
infile.open("argv[1]", ios_base::in);
改成
infile.open("text", ios_base::in);
也就是直接在code內把先打好要讀取檔案的名稱text
然後重新comple後
>> ./a.out
就可以讀取成功
想請問第一種方式是錯在哪裡呢?
萬分感謝! >"<
錯誤結果(Wrong Output):
reading file error!
程式碼(Code):
#include <iostream>
#include <fstream>
int main (int argc, char *argv[])
{
fstream infile;
infile.open("argv[1]", ios_base::in);
if (!infile)
cout << endl << "reading file error!" << endl;
else cout << endl << "reading file successfully"<< endl;
infile.close();
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.218.129
※ 編輯: pluseus 來自: 140.112.218.129 (09/01 23:17)
→
09/01 23:17, , 1F
09/01 23:17, 1F
→
09/01 23:20, , 2F
09/01 23:20, 2F
※ 編輯: pluseus 來自: 140.112.218.129 (09/01 23:21)
→
09/02 15:14, , 3F
09/02 15:14, 3F