#define _CRT_SECURE_NO_WARNINGS #include <iostream>#include<fstream>#include<cstdlib>usingnamespacestd;
int main()
{
char ch;
int count=0;
ifstream Love;
Love.open("E:/Her.txt");
cout << "Hi,I am a program , and I will open your heart~.\n";
if (!Love.is_open())//is_open()方法可以用来检测文件是否被正确打开,如果正确打开则返回ture
{
cout << "Oh,it's a sorrowful story...\n";
cout << "Wow...\n";
exit(EXIT_FAILURE);//终止程序,EXIT_FAILURE是同系统通信的参数值
}
ch = Love.get();
while (Love.good())
{
cout<<ch;
++count;
ch=Love.get();//后面并不需要cin.get(),因为此时文件中的换行符也是我们所需要的字符。
}
if (Love.eof())//当最后一次读取数据时遇到EOF时,方法eof()将返回true。//另外,有个fail()方法遇到类型不匹配的输入时会返回ture,但它在接受到EOF时也会返回ture。
{
cout << endl;
cout << "The counts are almost: " << count << endl;
}
Love.close();
return0;
}