I/O流+统计文件词频
body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
(2)高层I/O:高层I/O是在底层I/O的基础上扩展起来的,仍旧将外部设备和磁盘文件统一处理,但处理的方式更为灵活,提供的一组处理函数定义在头文件stdio.h中,新的C++标准头文件为<cstdio>,提供的这些函数大体可分为两类:一般文件函数(外部设备和磁盘文件)和标准I/O函数。
|
#include<iostream>
#include<string>
#include<limits>
using namespace std;
void print_cin()
{
cout<<"badbit="<<cin.bad()<<endl;
cout<<"failbit="<<cin.fail()<<endl;
cout<<"eofbit="<<cin.eof()<<endl;
cout<<"goodbit="<<cin.good()<<endl;
}
int main()
{
print_cin();
cout<<endl;
int num;
while(cin>>num)
cout<<"num="<<num<<endl;
print_cin();
cout<<endl;
cin.clear();
print_cin();
cout<<endl;
//cin.ignore(numeric_limits<streamsize>::max(),'\n');
//不加这句话,下面语句就会把刚才错误输入的字符串直接输出来
cin.ignore(1024,'\n');
//同上,表示最多忽略1024个字符,期间遇到'\n'就开始执行下面语句
string s;
cin>>s;
cout<<s<<endl;
return 0;
}
|
#include<iostream>
#include<limits>
using namespace std;
int main()
{
int num;
while(cin>>num,!cin.eof())
{
if(cin.good())
{
cout<<"num="<<num<<endl;
}
if(cin.fail())
{
cout<<"data error,try again"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//cin.clear(); 会出现死循环,必须在ignore前面使用
}
}
return 0;
}
|
|
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
ifstream in("file");
if(!in.good())
{
cout<<"open file fail!"<<endl;
return -1;
}
vector<string> vec;
vec.reserve(10);
string str;
while(getline(in,str))
cout<<str<<endl;
// vec.push_back(str);
cout<<endl;
in.close();
return 0;
}
|
使用迭代器
vector<string>::iterator p;
#if 1
for(p=vec.begin();p!=vec.end();p++)
{
cout<<*p<<endl;
}
#endif
|
|
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream in("file"); //要求文件事先存在
if(!in.good())
{
cout<<"open file fail!"<<endl;
return -1;
}
ofstream out("file1"); //对文件存不存在没有要求
if(!out.good())
{
cout<<"open file1 fail!"<<endl;
return -1;
}
string str;
while(getline(in,str))
{
out<<str<<endl;
}
in.close();
out.close();
return 0;
}
|
|
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream io("file") ; //要求文件事先存在
if(!io.good())
{
cout<<"open file fail!"<<endl;
return 0;
}
int num;
for(int i=0;i!=10;i++)
{
cin>>num;
io<<num<<" ";
}
io<<endl;
cout<<endl;
cout<<"io.tellp()="<<io.tellp()<<endl<<endl; //tellp()ifstream对象特有
io.seekp(0,ios::beg);
for(int i=0;i<10;i++)
{
io>>num;
cout<<num<<" ";
}
cout<<endl<<endl;
cout<<"io.tellg()="<<io.tellg()<<endl; //tellg(),ofstream对象特有
return 0;
}
|
|
#include <iostream>
#include<sstream>
#include<string>
#include<stdio.h>
#include<string.h>
using namespace std;
void sprint(int a,int b)
{
char arr[100];
memset(arr,0,sizeof(arr));
sprintf(arr,"%d,%d",a,b);
cout<<arr<<endl;
}
string ostring(int a,int b) //这里字符串,整形都可以
{
ostringstream oss;
//stringstream oss; //一样的效果
oss<<a<<","<<b;
}
void istring(string line)
{
istringstream iss(line);
//stringstream iss(line);
string world;
while(iss>>world)
{
cout<<world<<endl;
}
}
|
int main()
{
int a=512;
int b=1024;
sprint(a,b);
cout<<endl;
string s = ostring(a,b);
cout<<s<<endl<<endl;
string s1 = "hello world shen zhen";
istring(s1);
return 0;
}
|
|
3. 统计一篇英文(The_Holy_Bible.txt)文章中出现的单词和词频,
输入:某篇文章的绝对路径
输出:词典(词典中的内容为每一行都是一个“单词 词频”)
-----------------
| a 66 |
| abandon 77 |
| public 88 |
| ...... |
|_________________|
class WordStatic
{
public:
void read_file(std::string filename);
void write_file(std::string filename);
private:
//......
};
|
|
#include <iostream>
#include<fstream>
#include<vector>
#include<string>
#include<string.h>
#include<iomanip>
using namespace std;
class WordStatistic
{
private:
vector<string> word;
vector<int> count;
public:
void read_file(string filename);
void write_file(string filename);
};
void WordStatistic::read_file(string filename)
{
char name[100]; //这里不能定义指针,不然段错误
strcpy(name,filename.c_str());
ifstream in(name);
//括号里面的参数必须是const char*
if(!in.good())
{
cout<<"open file fail!"<<endl;
return ;
}
cout<<"正在统计文件……"<<endl;
string tmpword;
while(in>>tmpword)
{
if(tmpword[0]>='0'&&tmpword[0]<='9')
continue;
int vecwordsize=word.size();
int i;
for(i=0;i<vecwordsize;i++)
{
if(word[i].compare(tmpword)==0)
//if(vers[idx]==tmpword)
{
count[i]++;
break;
}
}
if(i==vecwordsize)
{
word.push_back(tmpword);
count.push_back(1);
}
}
in.close();
cout<<"统计结束!"<<endl;
}
|
void WordStatistic::write_file(string filename)
{
char name[100];
strcpy(name,filename.c_str());
ofstream out(name);
cout<<"统计结果正在写入文件……"<<endl;
if(out.good()==0)
{
cout<<"open outfile fail!"<<endl;
return ;
}
for(int i=0;i<word.size();i++)
{
out<<setw(20)<<word[i]<<setw(15)<<count[i]<<endl;
}
out.close();
cout<<"写入结束!"<<endl;
}
int main()
{
WordStatistic WS;
WS.read_file("The_Holy_Bible.txt");
WS.write_file("统计结果.txt");
return 0;
}
|
I/O流+统计文件词频的更多相关文章
- java IO流 对文件操作的代码集合
Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...
- week12 201621044079 流与文件
作业12-流与文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车 ...
- Java笔记13:统计文件中每个字符出现的次数
一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...
- 利用fgetc统计文件所在字节 和 总行数 和单词数
#include <stdio.h> #include <stdlib.h> #define IS_WHITE_SPACE(c) ((c)==' '||(c)=='\t'||( ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- C++之流与文件
C++中,输入输出采用流来进行,例如iostream库中的 cin 和 cout .对文件进行读写操作也使用流.可以将文件与流关联起来,然后对文件进行操作.要将流与文件关联起来,必须像声明变量那样声明 ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- java io流 创建文件、写入数据、设置输出位置
java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...
- java io流(字符流) 文件打开、读取文件、关闭文件
java io流(字符流) 文件打开 读取文件 关闭文件 //打开文件 //读取文件内容 //关闭文件 import java.io.*; public class Index{ public sta ...
随机推荐
- SpringMVC整合ActiveMQ
spring activeMq 配置 <!-- ActiveMQ 连接工厂 --> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服 ...
- 带你走进ajax(3)
使用ajax实现用户名有效性验证 需求:当用户输入完用户名时,用户可以通过页面上的按键来确认当前的用户名是否有效.如下图所示 思路:用户触发按键后可以向服务器发起http请求,将用户名提交给服务器来进 ...
- centos 升级nginx到1.10.2
之前装的是1.6.3版本,准备升级到1.10.2版本. 1.下载nginx1.10.2 wget http://nginx.org/download/nginx-1.10.2.tar.gz 2.解压缩 ...
- javascript 设置元素滚动大小
3. 滚动大小 最后要介绍的是滚动大小(scroll dimension),指的是包含滚动内容的元素的大小. 有些元素(例如 元素),即使没有执行任何代码也能自动地添加滚动条:但另外一些元素,则需要通 ...
- 如何用纯 CSS 创作一个摇摇晃晃的 loader
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/oyJvpe 可交互视频 此 ...
- 20145301 实验三 "敏捷开发与XP实践"
20145301 实验三 "敏捷开发与XP实践" 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.22 实验名称:敏捷开发与XP实践 一.实验 ...
- 20145230熊佳炜《网络对抗》实验五:MSF基础应用
20145230熊佳炜<网络对抗>实验五:MSF基础应用 主动攻击 首先,我们需要弄一个xp sp3 English系统的虚拟机,然后本次主动攻击就在我们kali和xp之间来完成. 然后我 ...
- Linux下安装SVN服务端
安装 使用yum安装非常简单: yum install subversion 配置 2.1. 创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个下 ...
- MysQL使用一高级应用(上)
简介 实体与实体之间有3种对应关系,这些关系也需要存储下来 在开发中需要对存储的数据进行一些处理,用到内置的一些函数 视图用于完成查询语句的封装 事务可以保证复杂的增删改操作有效 关系 创建成绩表sc ...
- KALI LINUX系统初始化配置
1.Kali Linux安装VirtualBox增强功能 VirtualBox增强功能介绍:物理机与虚拟机之间的文件共享.物理机与虚拟机之间的剪切板共享.虚拟机的direct3D支持,这样虚拟机窗口就 ...