C++文件输入输出流

来源:百度知道 编辑:UC知道 时间:2024/05/12 13:00:43
从文件mytest.txt读出全部内容,统计其中的单词(以空格为单词分隔符)个数,并打印单词个数及总文件长度。
评分标准:
(1)文件打开关闭正确(20%);
(2)能够从文件中读取数据(20%);
(3)计算结果正确(20%);
(4)输出格式美观(20%)。

#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace stdl;

int main()
{
ifstream fin("mytest.txt");
int num = 0;
if(!fin) return 1;
string word;
while(!fin.eof())
{
fin>>word;
num++;
}
fin.close();
ifstream ff("mytext.txt", ios_base::binary);
ff.seekg(0L, ios_base::end);
long size = ff.tellg();
ff.close();
cout<<setw(10)<<"Words"<<setw(10)<<"Size"<<endl;
cout<<setw(10)<<num<<setw(10)<<size<<endl;
return 0;

}