网上大神的简单代码
- #include<iostream> #include<fstream>
- using namespace std; int main()
- { fstream f("test.txt",ios::in);
- char c; int n=0;
- while(f.get(c))n++; cout<<n<<endl;
- f.close(); return 0;
- }
上面那方法会计算空格和换行,如果不想要换行和空格,可以这样:
- #include<iostream> #include<fstream>
- using namespace std; int main()
- { fstream f("test.txt",ios::in);
- char c; int n=0;
- while(f>>c)n++; cout<<n<<endl;
- f.close(); return 0;
- }










