起因
(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
//读取方式: 逐词读取, 词之间用空格区分(遇到空格认为本次读取结束),输出之后进行下一次读取
//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++文件处理对比)
#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的东东的
总结
第一条,(写了这么多了,用两句话概括吧)最近从网上,看到了一句很经典的话,c++的风fstream类+string类也可以非常好的处理文本文件;
第二条,语言仅仅是一种工具,本身并没有优劣之分
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有