C语言问题

# include<fstream>
# include<iostream>
# include<iomanip>
using namespace std;
int main(){
ifstream ifile;
ifile.open("d: \\my2file.txt");
int ch;
int nline=0,nword=0,nch=0;
int isword=0;
do{ch=ifile.get();
if(ch=='\n') nline++;
if(ch!=' '&&ch!='\t'&&ch!='\n'&&ch!=EOF){
if(! isword) nword++;
nch++;
isword=1;
}
else isword=0;
}while(ch!=EOF);
cout<<"行数:"<<nline<<endl;
cout<<"单词数:"<<nword<<endl;
cout<<"字符数:"<<nch<<endl;
ifile.close();
return 0;
}

运行的时候依然是
行数
单词数
字符数
都为0,请错在哪?D盘文件已经存在,名字也一样!

int ch;
ch=ifile.get();
你怎么用一个int型的来接受啊,你改成string ch 试试,不过你那个get()好像并不去除空格,你可以尝试用getline这个函数写一下,有什么问题你在发上来,我们在研究一下
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-10-30
(ch!=EOF);
老大 应该是(ch!==eof) !
第2个回答  2008-10-30
打开文件\\my2file.txt时没有确定其的读写方式,也就说什么东西都没写进去啊!应该是这个问题!你的程序可读性不高,要想容易发现错误就要修改啊!
相似回答