我正在學習文件流。在main之外的單獨函數中使用ifstream時遇到問題。內部主要它工作得很好。功能是這樣的,無法在單獨的函數中調用ifstream
void counter(string str)
{
ifstream inpufile(str,ios::in);//the error is here, it says the function cannot be called!?!
if(!inpufile)
{
cout<<"\n The file does not exist, try again";
}
else
{
char c;
int counter=0;
while(inpufile.get(c))
{
if(c=='\n')
counter++;
}
cout<<"\n The number of lines in the file are "<<counter+1;
}
inpufile.close();
}
函數調用是從主(),並且是這樣
counter(argv[1]);
我只能通過ifstream的作爲對象。我能否創建一個外部主體?
您是否在'counter'文件中包含'#include'? –
2014-10-09 15:02:18
是的,我做到了。它在文件的頂部。 – 2014-10-09 15:05:22
你的代碼似乎沒有錯。你是否收到編譯錯誤或運行時錯誤? – Paulo1205 2014-10-09 15:06:39