C++中文本文件读取优化-使用缓冲
2011-06-19 11:13
375 查看
C++中可以采用stream读取文本文件,基本方式是一次一行,编程简洁易行,比用C方便多了。但是,凡事有利有弊,当文件行数较多时,文件读取IO次数就会随之增加,文件读取的时间会急剧增长。因为文件IO的时间要远大于CPU在内存中处理数据的时间,假如IO时间是毫秒级的,那么CPU在内存处理数据是纳秒级的。
很显然,C++中文本文件读取优化要解决的基本问题之一就是减少IO次数,最常用的方法之一是缓冲,也就是一次性将数据会部读入内存之后再处理。例如:
1. C++文本文件读写程序(基本方式)
2. 使用缓冲的读写程序
后者比前者的读写速度要快很多倍。主要原因就是后者的文件IO次数远远少于前者。
3. Java中的缓冲应用
其实缓冲在各种语言中都广泛用到,目的不外乎加快数据读写速度,下面是Java中的文本文件读写程序:
4. 备注
上文中的示例不考虑文本读取后对文本行的处理,其实只是一个简单的复制程序。如果仅仅复制文本文件的话,也可以这样:
还可以这样:
参考文献:http://topic.csdn.net/u/20110616/11/7028e734-6e84-4704-8de7-61f14b884781.html
很显然,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)); }
参考文献:http://topic.csdn.net/u/20110616/11/7028e734-6e84-4704-8de7-61f14b884781.html
相关文章推荐
- c++使用getline和ifstream读取文件
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
- 使用ifstream和getline读取文件内容[c++]
- 使用ifstream和getline读取文件内容[c++]
- 使用带缓冲的字符流读取字符文件。
- c# winForm使用Aspose.Cells读取CSV文件中文乱码问题
- 使用ifstream和getline读取文件内容[c++]
- 利用python读取xml布局文件下面使用中文或者引用资源文件里面的中文,并且显示行号
- 使用ifstream和getline读取文件内容 c++
- 解决Java读取properties文件的中文问题的新办法(不使用native2ascii.exe及其他工具)
- c++怎么读取文件中的中文字符串的几种方法
- 使用ifstream和getline读取文件内容[c++] ZZ
- 使用ifstream和getline读取文件内容[c++]
- 使用ifstream和getline读取文件内容[c++]
- pyhton使用open函数读取包含中文路径文件大坑记
- C++使用缓存加速文件的读取
- 使用ifstream和getline读取文件内容[c++]
- [libexpat]_[XML处理]_[C/C++使用libexpat库以数据流(SAX模型)的方式读取(解析)大XML文件]
- pyhton使用open函数读取包含中文路径文件大坑记
- C++ 使用ifstream和getline读取文件内容