C++使用缓存加速文件的读取
来源:互联网 发布:sai是什么软件 编辑:程序博客网 时间:2024/05/23 00:00
C++中可以采用stream读取文本文件,基本方式是一次一行,编程简洁易行,比用C方便多了。但是,凡事有利有弊,当文件行数较多时,文件读取IO次数就会随之增加,文件读取的时间会急剧增长。因为文件IO的时间要远大于CPU在内存中处理数据的时间,假如IO时间是毫秒级的,那么CPU在内存处理数据是纳秒级的。很显然,C++中文本文件读取优化要解决的基本问题之一就是减少IO次数,最常用的方法之一是缓冲,也就是一次性将数据会部读入内存之后再处理。例如:
1. C++文本文件读写程序(基本方式)
- //C++ code:
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class file{
- public:
- //按行读数据,不分行写入另外一个文件
- bool fromShortToLong(string s_path,string d_path){
- string buffer;
- fstream infile(s_path);
- fstream outfile(d_path);
- if(!infile){
- cout << "Unable to open infile";
- exit(1); // terminate with error
- }
- if(!outfile){
- cout << "Unable to open outfile";
- exit(1); // terminate with error
- }do{
- outfile<<buffer;
- }while (getline(infile,buffer));
- cout<<"读写完成。"<<endl;
- system("pause");
- outfile.close();
- infile.close();
- }
- };
- void main(){
- file test;
- test.fromShortToLong("D://test_read.txt","D://test_write.txt");
- }
2. 使用缓冲的读写程序
- bool fromShortToLong(string s_path,string d_path)
- {
- string buffer;
- ifstream infile(s_path.c_str());
- ofstream outfile(d_path.c_str());
- if(!infile)
- {
- cout << "Unable to open infile";
- exit(1); // terminate with error
- }
- if(!outfile)
- {
- cout << "Unable to open outfile";
- exit(1); // terminate with error
- }
- buffer.assign(istreambuf_iterator<char>(infile),istreambuf_iterator<char>());
- stringstream strStr;
- strStr.str(buffer);
- string linestr;
- do
- {
- outfile<<linestr;
- }
- while (getline(strStr,linestr));
- cout<<"读写完成。"<<endl;
- return true;
- }
后者比前者的读写速度要快很多倍。主要原因就是后者的文件IO次数远远少于前者。
3. Java中的缓冲应用
其实缓冲在各种语言中都广泛用到,目的不外乎加快数据读写速度,下面是Java中的文本文件读写程序:
- //java code
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Txt {
- //按行读数据,不分行写入另外一个文件
- public Boolean fromShortToLong(String s_path,String d_path) throws IOException {
- Boolean scc=false;
- File file = new File(s_path);
- FileWriter fw=new FileWriter(d_path);
- try {
- BufferedReader bw = new BufferedReader(new FileReader(file));
- String line = null;
- while((line = bw.readLine()) != null){
- fw.write(line);
- }bw.close();
- fw.close();
- scc=true;
- } catch (IOException e) {
- scc=false;
- e.printStackTrace();
- }
- return scc;
- }
- }
4. 备注
上文中的示例不考虑文本读取后对文本行的处理,其实只是一个简单的复制程序。如果仅仅复制文本文件的话,也可以这样:
- #define BUF_SIZE 1024
- std::ifstream ifs("c://test_read.txt");
- std::ofstream ofs("c://test_write.txt");
- if (ifs.is_open())
- {
- char buf[BUF_SIZE] = {0};
- while(!ifs.read(buf, BUF_SIZE).eof())
- {
- ofs.write(buff, BUF_SIZE - 1);
- }
- }
还可以这样:
- void copy(const string& strInFile, const string& strOutFile)
- {
- ifstream ifs(strInFile.c_str());
- ofstream ofs(strOutFile.c_str());
- copy(istream_iterator<char>(ifs),istream_iterator<char>(), ostreambuf_iterator<char>(ofs));
- }
0 0
- C++使用缓存加速文件的读取
- WebView的使用小结--加载加速及缓存机制
- 缓存读取文件的两种方式
- [C#] 如何有效的使用C#读取文件
- 【nginx】读取缓存文件
- C中读取一行一行的读取文件
- Android创建缓存文件并使用流读取
- Java读取复制超大文件加速
- yum缓存文件的使用
- 使用tmpfs作缓存、临时文件存储加速
- JAVA多线程读取同一个文件,加速对文件内容的获取
- Linux使用C读取文件目录
- 使用C语言读取bmp文件数据
- [C/C++]读取文件的多种方式
- 读取jar包的文件,有缓存的问题
- php常用的加速缓存的扩展
- nginx如何读取缓存文件
- Properties读取文件缓存数据
- JS控制图片显示的大小(图片等比例缩放)
- gesture-imageview 重复 setImageBitmap 设置图片空白解决方案
- echarts例子中的tip
- 0x86内存管理寄存器
- 什么情况下用Hashtable而不是HashMap
- C++使用缓存加速文件的读取
- 预编译的对象
- Arduino开发福利——Visual Studio插件与函数速查工具
- CWinThread的使用方法
- 《自己动手写操作系统》第三章 pmtest9源码解析——中断处理程序
- <c:foreach>用法
- Android Junit
- 程序提供向后兼容性
- 编辑错误:error C2018: unknown character '0xa1'