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 ...
随机推荐
- FFmpeg 入门(4):线程分治
本文转自:FFmpeg 入门(4):线程分治 | www.samirchen.com 概览 上一节教程中,我们使用 SDL 的音频相关的函数来支持音频播放.SDL 起了一个线程来在需要音频数据的时候去 ...
- [Face++]Face初探——人脸检测
经过了强烈的思想斗争才把自己拖到图书馆做毕设T^T anyway, 因为毕设里面有人脸识别的部分,所以就想找个现成的api先玩玩,于是就找到最近很火的face++:http://www.faceplu ...
- Python3.x:BeautifulSoup()解析网页内容出现乱码
Python3.x:BeautifulSoup()解析网页内容出现乱码 问题: start_html = requests.get(all_url, headers=Hostreferer) Beau ...
- 第三方CSS安全吗?
原文:https://jakearchibald.com/201...翻译:疯狂的技术宅 本文首发微信公众号:jingchengyideng欢迎关注,每天都给你推送新鲜的前端技术文章 前一段时间,有很 ...
- 20145314郑凯杰 《Java程序设计》第2周学习总结 代码开始!
---恢复内容开始--- 20145314郑凯杰 <Java程序设计>第2周学习总结 代码开始! 教材学习内容总结 跟着教材的顺序开始总结我学过的内容: 1编辑.编译.运行教材上代码 这部 ...
- 彻底的卸载干净oracle 11g
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> H ...
- SpringBoot 打包为Docker进行
可以有两种方式: 1.dockerfile 2.maven docker 第一种方式:通过dockerfile打包Docker镜像 1.将dockerfile和 springboot打包的jar文件放 ...
- optind变量
1.这个变量是在什么地方定义的? 答:系统定义的 2.这个变量在什么场景下使用? 答:在解析命令行参数时会用到 3.这个变量存在的意义? 在每调用一次getopt()或getopt_long()类似函 ...
- CentOS7下安装VLC
用最新的CentOS7发现没有视频播放器,于是在http://pkgs.org/上查找,发现了nux dextop仓库上有,于是到他的官网上http://li.nux.ro/repos.html查了下 ...
- 从0开始 Java实习 黑白棋
黑白棋的设计 代码如下: import java.util.*; public class Chess{ char[][] chess = new char[16][16]; public stati ...