(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;(2)文件打开函数
在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件fstream.h。下面就把此类 的文件操作过程一一道来。(3)打开方式
ios::out 输出数据覆盖现有文件 (默认的写代开方式,文件不存在,创建之;若存在,则覆盖原来的内容)(4)文件指针定位
和C的文件操作方式不同的是,C++ I/O系统管理两个与一个文件相联系的指针。一个是读指针,它说明输入操作在文件中的位置;另一个是写指针,它下次写操作的位置。每次执行输入或输出时,相应的指针自动变化。所以,C++的文件定位分为读位置和写位置的定位,(5)fstream , stream ;ifstream,istream;ofstream,ostream等的关系
三:实战篇
(1)read word by word ;no write
//读取方式: 逐词读取, 词之间用空格区分(遇到空格认为本次读取结束),输出之后进行下一次读取 //read data from the file, Word By Word //when used in this manner, we'll get space-delimited bits of text from the file //but all of the whitespace that separated words (including newlines) was lost. void ReadDataFromFileWBW() { ifstream fin("data.txt"); string s; cout << "*****start*******" << endl; while( fin >> s ) { cout << "Read from file: " << s << endl; } cout << "*****over*******" << endl; }(2)read by line fin.getline(char*,n)
//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分 //If we were interested in preserving whitespace, //we could read the file in Line-By-Line using the I/O getline() function. void ReadDataFromFileLBLIntoCharArray() { ifstream fin("data.txt",ios::in);// 默认的打开模式就是ios::in ofstream fout("out.txt",ios::out);// 默认代开模式就是ios::out const int LINE_LENGTH = 100; char str[LINE_LENGTH]; fout << "****CharArray start******" << endl; cout << "****CharArray start******" << endl; fin.seekg(-20,ios::end);// -20表示从end向前移动20字节,汉字占两字节;20表示向后移动指针 while( fin.getline(str,LINE_LENGTH) ) { fout << str << endl; cout << "Read from file: " << str << "..." << endl;// ****str里面本身包含着换行的,原来是什么样子,现在保存的就是什么样子 } fout << "*****over*******" << endl; cout << "*****over*******" << endl; }(3) read by line fin.getline(fin,string)
//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分 //If you want to avoid reading into character arrays, //you can use the C++ string getline() function to read lines into strings void ReadDataFromFileLBLIntoString() { ifstream fin("data.txt",ios::in);// 默认的打开模式就是ios::in ofstream fout("out.txt",ios::app);// 追加到文件尾的方式打开 string s; cout << "****start******" << endl; while( getline(fin,s) ) { fout << s << endl;// ofstream是默认,若文件不存在,则先建立此文件,并且再向文件写的过程中换行已经不存在了,这与cout控制台输出一样哦。。。 cout << "Read from file: " << s << endl;//****s同str里面本身已经没有了换行的,这和原来的getline()函数是一样的;数据原来是什么样子,现在保存的就是什么样子 } fout << "*****over*******" << endl; cout << "*****over*******" << endl; fout.close(); }
(4) main函数
#include <iostream> #include <fstream> #include <string> #include <cstdlib>// exit()函数 using namespace std; //输出空行 void OutPutAnEmptyLine() { cout<<"\n"; } //带错误检测的读取方式 //Simply evaluating an I/O object in a boolean context will return false //if any errors have occurred void ReadDataWithErrChecking() { string filename = "dataFUNNY.txt"; ifstream fin( filename.c_str()); if( !fin ) { cout << "Error opening " << filename << " for input" << endl; exit(-1); } } int main() { ReadDataFromFileWBW(); //逐词读入字符串 OutPutAnEmptyLine(); //输出空行 ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组 OutPutAnEmptyLine(); //输出空行 ReadDataFromFileLBLIntoString(); //逐词读入字符串 OutPutAnEmptyLine(); //输出空行 ReadDataWithErrChecking(); //带检测的读取 return 0; }
data文本文件的数据格式
(插入c 与 c++文件处理对比)
<p>#include <iostream> #include <fstream> #include <cassert> #include <ctime> #include <cstdio> using namespace std;</p><p>void test_read() { const char* read_file = "D:\\zyp\\大数据实验<a target=_blank href="file://\\TOKEN_ENEX_201404_20W_yuansi.csv">\\TOKEN_ENEX_201404_20W_yuansi.csv</a>"; const int BUF_SIZE = 1024 ; char buf[BUF_SIZE]; //c++ style writing file ifstream ifs(read_file,ios::binary); assert(ifs); time_t start, end; start = clock(); while(!ifs.eof()) { ifs.read(buf,BUF_SIZE); } end = clock(); ifs.close(); cout<<"C++ style: "<<end - start <<" ms"<<endl; //c style writing file FILE* fp = fopen(read_file, "rb"); start = clock(); int len = 0; char *pline = NULL; do { //len = fread(buf,1,BUF_SIZE,fp); pline = fgets(buf,BUF_SIZE,fp); cout << buf << endl; //cout<<len<<endl; }while(pline != NULL); end = clock(); fclose(fp); cout<<"C style: "<<end - start <<" ms"<<endl; cin.get(); } int main() { test_read(); return 0; }</p>总之,/fread() 的效率 是 ifstream.read()的将近十倍!但是,fstream 对于处理数据而言,还是统一的应用STL的标准好;总之,语言仅仅是一门工具,本身没有优劣之分。fgets(char*,int,File*); getline(stream,string),还是个人习惯的好。如今,还是建议应用面向对象的语言好一些,java 或者 c++,java更加强大一些,有自己的各种类库。c++没有的,链接JDBC的东东的
(5)总结
第一条,(写了这么多了,用两句话概括吧)最近从网上,看到了一句很经典的话,c++的风fstream类 + string类 也可以非常好的处理文本文件;