C++使用缓存加速文件的读取

来源:互联网 发布:sai是什么软件 编辑:程序博客网 时间:2024/05/23 00:00

C++中可以采用stream读取文本文件,基本方式是一次一行,编程简洁易行,比用C方便多了。但是,凡事有利有弊,当文件行数较多时,文件读取IO次数就会随之增加,文件读取的时间会急剧增长。因为文件IO的时间要远大于CPU在内存中处理数据的时间,假如IO时间是毫秒级的,那么CPU在内存处理数据是纳秒级的。

    很显然,C++中文本文件读取优化要解决的基本问题之一就是减少IO次数,最常用的方法之一是缓冲,也就是一次性将数据会部读入内存之后再处理。例如:

1. C++文本文件读写程序(基本方式)

[c-sharp] view plaincopyprint?
  1. //C++ code:  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include <string>  
  5. using namespace std;  
  6. class file{  
  7. public:  
  8.     //按行读数据,不分行写入另外一个文件  
  9.     bool fromShortToLong(string s_path,string d_path){  
  10.         string buffer;  
  11.         fstream infile(s_path);  
  12.         fstream outfile(d_path);  
  13.         if(!infile){  
  14.             cout << "Unable to open infile";  
  15.             exit(1); // terminate with error  
  16.         }  
  17.         if(!outfile){  
  18.             cout << "Unable to open outfile";  
  19.             exit(1); // terminate with error  
  20.         }do{  
  21.             outfile<<buffer;  
  22.         }while (getline(infile,buffer));  
  23.         cout<<"读写完成。"<<endl;  
  24.         system("pause");  
  25.         outfile.close();  
  26.         infile.close();  
  27.     }  
  28. };  
  29. void main(){  
  30.     file test;  
  31.     test.fromShortToLong("D://test_read.txt","D://test_write.txt");  
  32. }  
 

2. 使用缓冲的读写程序

[c-sharp] view plaincopyprint?
  1. bool fromShortToLong(string s_path,string d_path)  
  2. {  
  3.     string buffer;  
  4.     ifstream infile(s_path.c_str());  
  5.     ofstream outfile(d_path.c_str());  
  6.     if(!infile)  
  7.     {  
  8.         cout << "Unable to open infile";  
  9.         exit(1); // terminate with error  
  10.     }  
  11.     if(!outfile)  
  12.     {  
  13.         cout << "Unable to open outfile";  
  14.         exit(1); // terminate with error  
  15.     }  
  16.     buffer.assign(istreambuf_iterator<char>(infile),istreambuf_iterator<char>());  
  17.     stringstream strStr;  
  18.     strStr.str(buffer);  
  19.     string linestr;  
  20.     do  
  21.     {  
  22.         outfile<<linestr;  
  23.     }  
  24.     while (getline(strStr,linestr));  
  25.     cout<<"读写完成。"<<endl;  
  26.     return true;  
  27. }  
 

后者比前者的读写速度要快很多倍。主要原因就是后者的文件IO次数远远少于前者。

3. Java中的缓冲应用

    其实缓冲在各种语言中都广泛用到,目的不外乎加快数据读写速度,下面是Java中的文本文件读写程序:

[java] view plaincopyprint?
  1. //java code  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. public class Txt {    
  8.     //按行读数据,不分行写入另外一个文件  
  9.     public Boolean fromShortToLong(String s_path,String d_path) throws IOException {  
  10.         Boolean scc=false;  
  11.         File file = new File(s_path);  
  12.         FileWriter fw=new FileWriter(d_path);  
  13.         try {  
  14.             BufferedReader bw = new BufferedReader(new FileReader(file));  
  15.             String line = null;  
  16.             while((line = bw.readLine()) != null){  
  17.                 fw.write(line);  
  18.             }bw.close();  
  19.             fw.close();  
  20.             scc=true;  
  21.         } catch (IOException e) {  
  22.             scc=false;  
  23.             e.printStackTrace();  
  24.         }  
  25.         return scc;  
  26.     }  
  27. }  
 

4. 备注

    上文中的示例不考虑文本读取后对文本行的处理,其实只是一个简单的复制程序。如果仅仅复制文本文件的话,也可以这样:

[c-sharp] view plaincopyprint?
  1. #define BUF_SIZE 1024  
  2. std::ifstream ifs("c://test_read.txt");  
  3. std::ofstream ofs("c://test_write.txt");  
  4. if (ifs.is_open())  
  5. {  
  6.     char buf[BUF_SIZE] = {0};  
  7.     while(!ifs.read(buf, BUF_SIZE).eof())  
  8.     {  
  9.         ofs.write(buff, BUF_SIZE - 1);  
  10.     }  
  11. }  

    还可以这样:

[c-sharp] view plaincopyprint?
  1. void copy(const string& strInFile, const string& strOutFile)  
  2. {  
  3.     ifstream ifs(strInFile.c_str());  
  4.     ofstream ofs(strOutFile.c_str());  
  5.     copy(istream_iterator<char>(ifs),istream_iterator<char>(), ostreambuf_iterator<char>(ofs));  
  6. }  
 

0 0
原创粉丝点击