你好,游客 登录
背景:
阅读新闻

c++ fstream + string处理大数据

[日期:2014-12-17] 来源:CSDN博客  作者:瞭望天空 [字体: ]

一:起因

(1)之前处理文本数据时,各种清洗数据用的都是java的File,FileReader/FileWriter,BufferedReader/BufferedWriter等类,详见java读写文件

(2)应用java的原因是java里面的map非常灵活,eclipse编译器更是给力,而且ctrl 可以追踪函数 等,详见java map的排序

(3)应用java的另一个原因是java里面的string类的字符串处理非常灵活,各种函数是应用尽有。

(4)上面两点算是自己的误解吧,因为c++里面也有也有与之对应的 fstream类,  c++map容器类,详见c++ map简介

(5)c++里面也有相对比较成熟的string类,里面的函数也大部分很灵活,没有的也可以很容易的实现split,strim等,详见c++string 实现

(6)最近从网上,看到了一句很经典的话,c++的风fstream类 + string类 也可以非常好的处理文本文件,让我们一起来见证

二:fstream的前世今生

 

(1)简介

   包含的头文件    #include <fstream>                using namespace std;                                                      
   C++中的三个文件流                                                          
   a ---- ofstream ofs("文件名", 打开方式);                b ---- ifstream ifs("文件名", 打开方式);     c ---- fstream fs("文件名",输入打开方式 | 输出打开方式);   三种文件      流分别用于写文件、读文件、读写文件 ,一般用a b两种方式进行,因为一个文件同时进行读写的情况采用c 方式。
   三种文件流都可先定义,再打开文件,以fstream为例                            
   fstream fs;         fs.open("文件名", 输入打开方式 | 输出打开方式);                            
   其中“打开方式”可以不给出。     若不给出,对于oftream默认为ios::out,iftream默认为ios::in 

(2)文件打开函数

在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件fstream.h。下面就把此类的文件操作过程一一道来。
打开文件在fstream类中,有一个成员函数open(),就是用来打开文件的,其原型是:
  void open(const char* filename,int mode,int access);

  参数:
  filename:     要打开的文件名
  mode:         要打开文件的方式
  access:       打开文件的属性

(3)打开方式                                                                  

   ios::out 输出数据覆盖现有文件 (默认的写代开方式,文件不存在,创建之;若存在,则覆盖原来的内容)
   ios::app 输出数据填加之现有文件末尾  (追加末尾写代开方式,不覆盖原内容)
 
   ios::ate 打开文件并移动文件指针至末尾                                    
   ios::in     打开文件以输入 (默认读的打开方式) 
   ios::trunc   输出文件中现有内容(ios::out的默认操作)                        
   ios::binary 二进制打开供读写

(4)文件指针定位

和C的文件操作方式不同的是,C++ I/O系统管理两个与一个文件相联系的指针。一个是读指针,它说明输入操作在文件中的位置;另一个是写指针,它下次写操作的位置。每次执行输入或输出时,相应的指针自动变化。所以,C++的文件定位分为读位置和写位置的定位,
对应的成员函数是 seekg()和seekp(),seekg()是设置读位置,seekp是设置写位置。它们最通用的形式如下: 

istream &seekg(streamoff offset,seek_dir origin); 
ostream &seekp(streamoff offset,seek_dir origin); 

streamoff定义于 iostream.h 中,定义有偏移量 offset 所能取得的最大值,seek_dir 表示移动的基准位置,是一个有以下值的枚举:
ios::beg:  文件开头 
ios::cur:  文件当前位置 
ios::end:  文件结尾 

这两个函数一般用于二进制文件,因为文本文件会因为系统对字符的解释而可能与预想的值不同。 
例: 
file1.seekg(1234,ios::cur);//把文件的读指针从当前位置向后移1234个字节
file2.seekp(1234,ios::beg);//把文件的写指针从文件开头向后移1234个字节
file1.seekg(-128,ios::end); //把文件的读指针从文件末尾向前移128个字节

注意:一个汉字是占用两个字节的,一个字母占用一个字节。

(5)fstream , stream ;ifstream,istream;ofstream,ostream等的关系

 


三:实战篇

(1)read word by word ;no write

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  //读取方式: 逐词读取, 词之间用空格区分(遇到空格认为本次读取结束),输出之后进行下一次读取 
  2.  //read data from the file, Word By Word 
  3.  //when used in this manner, we'll get space-delimited bits of text from the file 
  4.  //but all of the whitespace that separated words (including newlines) was lost. 
  5. void ReadDataFromFileWBW() 
  6.     ifstream fin("data.txt"); 
  7.     string s; 
  8.     cout << "*****start*******" << endl; 
  9.     while( fin >> s ) 
  10.     { 
  11.           cout << "Read from file: " << s << endl; 
  12.     } 
  13.     cout << "*****over*******" << endl; 

(2)read by line  fin.getline(char*,n)

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  //读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分 
  2.  //If we were interested in preserving whitespace, 
  3.  //we could read the file in Line-By-Line using the I/O getline() function. 
  4. void ReadDataFromFileLBLIntoCharArray() 
  5.     ifstream fin("data.txt",ios::in);// 默认的打开模式就是ios::in 
  6.     ofstream fout("out.txt",ios::out);// 默认代开模式就是ios::out 
  7.     const int LINE_LENGTH = 100; 
  8.     char str[LINE_LENGTH]; 
  9.     fout << "****CharArray start******" << endl; 
  10.     cout << "****CharArray start******" << endl; 
  11.     fin.seekg(-20,ios::end);// -20表示从end向前移动20字节,汉字占两字节;20表示向后移动指针 
  12.     while( fin.getline(str,LINE_LENGTH) ) 
  13.     { 
  14.         fout << str << endl; 
  15.         cout << "Read from file: " << str << "..." << endl;// ****str里面本身包含着换行的,原来是什么样子,现在保存的就是什么样子 
  16.     } 
  17.     fout << "*****over*******" << endl; 
  18.     cout << "*****over*******" << endl; 

(3) read by line fin.getline(fin,string)

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  //读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分 
  2.  //If you want to avoid reading into character arrays, 
  3.  //you can use the C++ string getline() function to read lines into strings 
  4. void ReadDataFromFileLBLIntoString() 
  5.     ifstream fin("data.txt",ios::in);// 默认的打开模式就是ios::in 
  6.     ofstream fout("out.txt",ios::app);// 追加到文件尾的方式打开 
  7.     string s; 
  8.     cout << "****start******" << endl; 
  9.     while( getline(fin,s) ) 
  10.     { 
  11.         fout << s << endl;// ofstream是默认,若文件不存在,则先建立此文件,并且再向文件写的过程中换行已经不存在了,这与cout控制台输出一样哦。。。 
  12.         cout << "Read from file: " << s << endl;//****s同str里面本身已经没有了换行的,这和原来的getline()函数是一样的;数据原来是什么样子,现在保存的就是什么样子 
  13.     } 
  14.     fout << "*****over*******" << endl; 
  15.     cout << "*****over*******" << endl; 
  16.     fout.close(); 

(4) main函数

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <cstdlib>// exit()函数 
  5.  
  6. using namespace std; 
  7.  
  8.  //输出空行 
  9. void OutPutAnEmptyLine() 
  10.       cout<<"\n"
  11.  
  12.  //带错误检测的读取方式 
  13.  //Simply evaluating an I/O object in a boolean context will return false 
  14.  //if any errors have occurred 
  15. void ReadDataWithErrChecking() 
  16.     string filename = "dataFUNNY.txt"
  17.     ifstream fin( filename.c_str()); 
  18.     if( !fin ) 
  19.     { 
  20.           cout << "Error opening " << filename << " for input" << endl; 
  21.           exit(-1); 
  22.     } 
  23.  
  24. int main() 
  25.       ReadDataFromFileWBW(); //逐词读入字符串 
  26.       OutPutAnEmptyLine(); //输出空行 
  27.  
  28.       ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组 
  29.       OutPutAnEmptyLine(); //输出空行 
  30.  
  31.       ReadDataFromFileLBLIntoString(); //逐词读入字符串 
  32.       OutPutAnEmptyLine(); //输出空行 
  33.  
  34.       ReadDataWithErrChecking(); //带检测的读取 
  35.      return 0; 

data文本文件的数据格式


 (5)总结

第一条,(写了这么多了,用两句话概括吧)最近从网上,看到了一句很经典的话,c++的风fstream类 + string类 也可以非常好的处理文本文件;
第二条,语言仅仅是一种工具,本身并没有优劣之分

原文链接:http://blog.csdn.net/u010700335/article/details/41621187





收藏 推荐 打印 | 录入: | 阅读:
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款