赞
踩
ios_base <- ios <- istream <- ifstream <- fstream
<- ostream <- ofstream <- fstream
C++ 使用标准库类来处理面向流的输入和输出:
iostream 控制台读写
fstream 文件读写
stringstream 内存string 的读写
每个IO 对象都维护一组条件状态 flags (eofbit, failbit and badbit),用来指出此对象上是否可以进行 IO 操作。如果遇到错误—例如输入流遇到了文件末尾,则对象的状态变为是失效,所有的后续输入操作都不能执行,直到错误纠正。
1 #include <fstream>
2 ofstream //文件写操作
3 ifstream //文件读操作
4 fstream //文件读写操作
成员函数
Public member functions
1, 构造函数
第一种不绑定文件,后续用open() 绑定。 即:ifstream in;
第二种绑定文件 filename ,读取模式默认参数为 ios::in可以省略。
//第1种
ifstream in;
in.open("H:\\ProductCode\\FileText\\clientc.txt",ios::in);
//第2种
ifstream in("H:\\ProductCode\\FileText\\clientc.txt",ios::in);
//函数原型:
explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in);
2,ifstream::open
打开文件filename,模式默认 ios_base::in
void open (const char* filename, ios_base::openmode mode = ios_base::in);
void open (const string& filename, ios_base::openmode mode = ios_base::in);
函数参数:
filename 要打开文件的文件名
mode 打开文件的方式
prot 打开文件的属性
member constant | stands for | access |
---|---|---|
in | input File | 读的方式打开文件 |
out | output | 写的方式打开文件 |
binary | binary | 二进制方式打开 |
ate | at end | 打开的时候定位到文件末尾 |
app | append | 所有操作都定位到文件末尾 |
trunc | truncate | 丢弃打开前文件存在的内容 |
表:文件打开模式标记
模式标记 | 适用对象 | 作用 |
---|---|---|
ios::in | ifstream fstream | 打开文件用于读取数据。如果文件不存在,则打开出错。 |
ios::out | ofstream fstream | 打开文件用于写入数据。如果文件不存在,则新建该文件;如 果文件原来就存在,则打开时清除原来的内容。 |
ios::app | ofstream fstream | 打开文件,用于在其尾部添加数据。如果文件不存在,则新建该文件。 |
ios::ate | ifstream | 打开一个已有的文件,并将文件读指针指向文件末尾(读写指 的概念后面解释)。如果文件不存在,则打开出错。 |
ios:: trunc | ofstream | 单独使用时与 ios:: out 相同。 |
ios::binary | ifstream ofstream fstream | 以二进制方式打开文件。若不指定此模式,则以文本模式打开。 |
ios::in ios::out | fstream | 打开已存在的文件,既可读取其内容,也可向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。 |
ios::in ios::out | ofstream | 打开已存在的文件,可以向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。 |
ios::in ios::out ios::trunc | fstream | 打开文件,既可读取其内容,也可向其写入数据。如果文件本来就存在,则打开时清除原来的内容;如果文件不存在,则新建该文件。 |
ios::binary 可以和其他模式标记组合使用,例如:
ios::in | ios::binary表示用二进制模式,以读取的方式打开文件。
ios::out | ios::binary表示用二进制模式,以写入的方式打开文件。
打开文件的属性同样在ios类中也有定义:
0 | 普通文件打开操作 |
---|---|
1 | 只读文件 |
2 | 隐含文件 |
4 | 系统文件 |
对于文件的属性也可以使用“或”运算和“+”进行组合使用,这里就不做说明了。
很多程序中,可能会碰到ofstream out(“Hello.txt”), ifstream in("…"),fstream foi("…")这样的的使用,并没有显式的去调用open()函数就进行文件的操作,直接调用了其默认的打开方式,因为在stream类的构造函数中调用了open()函数,并拥有同样的构造函数,所以在这里可以直接使用流对象进行文件的操作,默认方式如下:
ofstream out("...", ios::out);
ifstream in("...", ios::in);
fstream foi("...", ios::in|ios::out);
当使用默认方式进行对文件的操作时,你可以使用成员函数is_open()对文件是否打开进行验证
3,ifstream:: is_open
bool is_open() const;
文件流对象与文件绑定,返回 true ,否则 false 。
void close(); //关闭文件流
5,ifstream:: rdbuf
filebuf* rdbuf() const;
返回一个 filebuf 对象指针,(The pointer to the internal filebuf object.)
6,ifstream:: operator =
copy(1) ifstream& operator= (const ifstream&) = delete;
move(2) ifstream& operator= (ifstream&& rhs);
等号运算符禁止使用左值引用,可以使用右值引用。(即右边的值必须是一个即将销毁的临时对象)
Public member functions inherited from istream
7,std::istream::operator>>
输入终端 cin 和 ifstream 都是 istream 的子类,所以输入操作符 >> 用法相同。对变量进入输入的时候重载了常用的数据类型。
arithmetic types (1) istream& operator>> (bool& val); istream& operator>> (short& val); istream& operator>> (unsigned short& val); istream& operator>> (int& val); istream& operator>> (unsigned int& val); istream& operator>> (long& val); istream& operator>> (unsigned long& val); istream& operator>> (long long& val); istream& operator>> (unsigned long long& val); istream& operator>> (float& val); istream& operator>> (double& val); istream& operator>> (long double& val); istream& operator>> (void*& val); stream buffers (2) istream& operator>> (streambuf* sb ); manipulators (3) istream& operator>> (istream& (*pf)(istream&)); istream& operator>> (ios& (*pf)(ios&)); istream& operator>> (ios_base& (*pf)(ios_base&));
8,istream::gcount
streamsize gcount() const;
返回最后一个输入操作读取的字符数目。
可以修改这个返回值的函数有:get,getline,ignore,peek,read, readsome,putback and unget. 其中函数peek, putback and unget 被调用后gcount()返回值为0。
9,istream::get
single character (1): //读取一个字符,遇到'\n',也从流中取出。
int get(); //字符按 int 返回
istream& get (char& c); // 读到c中
//读取n个 c 风格字符串到数组s中,遇到'\n'(或delim)停止读取,并把'\n'留在输入流中,若要读取多行,就要需要int get() 来取出'\n',才能读下一行。
c-string (2):
istream& get (char* s, streamsize n);//默认delim是换行字符'\n'
istream& get (char* s, streamsize n, char delim) //指定读取停止字符 delim
stream buffer (3): //内容读取到 streambuf 对象中。
istream& get (streambuf& sb);//默认delim是换行字符'\n'
istream& get (streambuf& sb, char delim);//指定读取停止字符 delim
下面的程序演示get()读取到streambuf 的用法。
#include <iostream> // std::cout, std::streambuf, std::streamsize #include <fstream> // std::ifstream using namespace std; int main () { std::ifstream ifs ("test.txt"); std::ofstream ofs ("out.txt"); std::streambuf *pbuf = ofs.rdbuf(); ifs.get(*pbuf);//默认读取截止字符是'\n', 所以读取一行停止,且没有读取'\n'。 pbuf->sputc(ifs.get()); // '\n'并没有被读取到pbuf,所以需要get()来读取'\n',然后用函数sputc()加到 pbuf 中。 ifs.get(*pbuf); // 从流中取出了'\n' ,才能读取第二行 pbuf->sputc(ifs.get()); /* 上面使用了函数 istream& get (streambuf& sb); 之后不能使用 istream& get (char* s, streamsize n); */ char s[20]; ifs.get(s,20);//虽然输入流有第三行,但是没法读取。 cout<<"get:"<<s<<endl; //内容为空 ofs.close(); ifs.close(); return 0; }
读取一行到字符数组。
istream& getline (char* s, streamsize n );
//默认delim是换行字符'\n',遇到后丢弃,第二次读取从delim后开始读。
istream& getline (char* s, streamsize n, char delim );
//自己定义停止符delim
字符串头文件也定义了从流中读取一行的函数 getline()
因为它不是流的成员函数,所以不能通过点访问。
std::getline (string)
(1) 用户定义截止字符
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim); //c++11 标准
(2) 截止字符默认'\n'
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str); // c++11 标准
用法:
从流对象is中读取一行存到字符串str 直到遇到截止字符,如果遇到截止字符,则把它从流中取出来,然后丢弃(它不被存储,下一个操作的起点在它之后)函数调用前str 中的内容将被覆盖。
demo: 读取文件流的内容
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string str; ifstream ifs("test.txt"); if(!ifs){ cout<<"open file fail!"<<endl; return 1; } while( getline(ifs,str)) { cout<<str<<endl; } return 0; }
11,istream::ignore
istream& ignore (streamsize n = 1, int delim = EOF);
从输入流中读取n个字符并且丢弃,或者读到delim字符再停止读取。
12,istream::peek
int peek();
返回输入流下一个字符,并把它留在输入流中,作为下一次读取的起点。返回值是整形ascll码值,可以用 char© 转化为字符。
13,istream::read
istream& read (char* s, streamsize n);
从输入流中提取n个字符,并把他们存数组s中,不检测内容,也不加字符串结尾符号‘\0’,实例:
// read a file into memory #include <iostream> // std::cout #include <fstream> // std::ifstream int main () { std::ifstream is ("test.txt", std::ifstream::binary); if (is) { // get length of file: is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); char * buffer = new char [length]; std::cout << "Reading " << length << " characters... "; // read data as a block: is.read (buffer,length); if (is) std::cout << "all characters read successfully."; else std::cout << "error: only " << is.gcount() << " could be read"; is.close(); // ...buffer contains the entire file... delete[] buffer; } return 0; }
14,istream::putback
istream& putback (char c);
// 用法,从输入流读取一个字符,再把它返回。
char c = std::cin.get();
std::cin.putback (c);
15,istream::unget
istream& unget();
// 返回最后一次读取的字符到输入流,类似putback()
char c = std::cin.get();
std::cin.unget();
16,istream::tellg
读取输入流中文件指针的位置,返回值可转化为 int。
streampos tellg();
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
17,istream::seekg
设定输入流中文件指针的位置。(1) 绝对位置 (2) 相对位置
(1)istream& seekg (streampos pos);
(2)istream& seekg (streamoff off, ios_base::seekdir way);
参数 pos 是流中的绝对位置可以转化为 int
参数 off 是偏移量,与way相关,类型是 int
参数 way 可以选下表中的任意一个常量。
value | offset is relative to… |
---|---|
ios_base::beg | beginning of the stream |
ios_base::cur | current position in the stream |
ios_base::end | end of the stream |
————————————————
18,ios::good
bool good() const;
bool eof() const;
bool fail() const;
bool bad() const;
检测流的状态是否正常。当错误的状态flags (eofbit, failbit and badbit) 都没被设置的时候返回true
特定的错误状态可以用下面的函数(eof, fail, and bad)来检测。
iostate value (member constant) | indicates | good() | eof() | fail() | bad() | rdstate() |
---|---|---|---|---|---|---|
goodbit | No errors (zero value iostate) | true | false | false | false | goodbit |
eofbit | End-of-File reached on input operation | false | true | false | false | eofbit |
failbit | Logical error on i/o operation | false | false | true | false | failbit |
badbit | Read/writing error on i/o operation | false | false | true | true | badbit |
19,ios::operator!
bool operator!() const;
//Returns true if either failbit or badbit is set, and false otherwise.
// 有错误状态返回 true
int main () {
std::ifstream is;
is.open ("test.txt");
if (!is)
std::cerr << "Error opening 'test.txt'\n";
return 0;
}
20,ios::operator bool
布尔运算: 当流对象单独出现在条件语句中时,就间接调用布尔运算。
如:if(ios), while(ios)
函数原型:
c++98: operator void*() const;
c++11: explicit operator bool() const;
返回值:failbit 或 badbit 都没被标记的时候返回真。
(对比good(): failbit 或 badbit 或 eofbit 都没被标记的时候返回真)
布尔运算一个很方便的用法就是检测文件结束。读到文件末尾的时候, eofbit, failbit 同时被设置为1,所以可以使用bool()来判断流的状态。
当文件打开失败的时候failbit 位被设置为1,所以也能检测打开是否成功。
//按行读文件,简洁的模板 #include<iostream> #include<fstream> #include<string> using namespace std; void print_state (const std::ios& stream) { cout << "good()=" << stream.good(); cout << " eof()=" << stream.eof(); cout << " fail()=" << stream.fail(); cout << " bad()=" << stream.bad()<<endl; } int main() { string str; ifstream ifs("test.txt"); if(ifs) { //while( bool(getline(ifs,str)))// 等价 //while( getline(ifs,str).good())//等价 while( getline(ifs,str)) { cout<<"line:"<<str<<endl; } } else{ cout<<"open file fail!"<<endl; return 1; } print_state(ifs); return 0; }
21,ios::rdstate
iostate rdstate() const;
// Returns the current internal error state flags of the stream.
// 返回当前流中的内部错误状态,iostate二进制数,需要做位运算来获取其相应位置上的值。
//这个函数的功能可以被 good(),eof(),fail(),bad() 替换。
int main () {
std::ifstream is;
is.open ("test.txt");
if ( (is.rdstate() & std::ifstream::failbit ) != 0 )
std::cerr << "Error opening 'test.txt'\n";
return 0;
}
状态标志符的验证(Verification of state flags)
除了eof()以外,还有一些验证流的状态的成员函数(所有都返回bool型返回值):
bad()
如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。
fail()
除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。
eof()
如果读文件到达文件末尾,返回true。
good()
这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。
要想重置以上成员函数所检查的状态标志,你可以使用成员函数clear(),没有参数。
获得和设置流指针(get and put stream pointers)
所有输入/输出流对象(i/o streams objects)都有至少一个流指针:
ifstream, 类似istream, 有一个被称为get pointer的指针,指向下一个将被读取的元素。
ofstream, 类似 ostream, 有一个指针 put pointer ,指向写入下一个元素的位置。
fstream, 类似 iostream, 同时继承了get 和 put
我们可以通过使用以下成员函数来读出或配置这些指向流中读写位置的流指针:
tellg() 和 tellp()
这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get 流指针的位置 (用tellg) 或 put 流指针的位置(用tellp).
seekg() 和seekp()
这对函数分别用来改变流指针get 和put的位置。两个函数都被重载为两种不同的原型:
seekg ( pos_type position ); seekp ( pos_type position );
使用这个原型,流指针被改变为指向从文件开始计算的一个绝对位置。要求传入的参数类型与函数 tellg 和tellp 的返回值类型相同。
seekg ( off_type offset, seekdir direction ); seekp ( off_type offset, seekdir direction );
使用这个原型可以指定由参数direction决定的一个具体的指针开始计算的一个位移(offset)。它可以是:
ios::beg | 从流开始位置计算的位移 |
---|---|
ios::cur | 从流指针当前位置开始计算的位移 |
ios::end | 从流末尾处开始计算的位移 |
流指针 get 和 put 的值对文本文件(text file)和二进制文件(binary file)的计算方法都是不同的,因为文本模式的文件中某些特殊字符可能被修改。由于这个原因,建议对以文本文件模式打开的文件总是使用seekg 和 seekp的第一种原型,而且不要对tellg 或 tellp 的返回值进行修改。对二进制文件,你可以任意使用这些函数,应该不会有任何意外的行为产生。
以下例子使用这些函数来获得一个二进制文件的大小:
// obtaining file size #include <iostream.h> #include <fstream.h> const char * filename = "test.txt"; int main () { long l,m; ifstream in(filename, ios::in|ios::binary); l = in.tellg(); in.seekg (0, ios::end); m = in.tellg(); in.close(); cout << "size of " << filename; cout << " is " << (m-l) << " bytes.\n"; return 0; } //结果: size of example.txt is 40 bytes.
4.二进制文件
在二进制文件中,使用<< 和>>,以及函数(如getline)来操作符输入和输出数据,没有什么实际意义,虽然它们是符合语法的。
文件流包括两个为顺序读写数据特殊设计的成员函数:write 和 read。第一个函数 (write) 是ostream 的一个成员函数,都是被ofstream所继承。而read 是istream 的一个成员函数,被ifstream 所继承。类 fstream 的对象同时拥有这两个函数。它们的原型是:
write ( char * buffer, streamsize size ); read ( char * buffer, streamsize size );
这里 buffer 是一块内存的地址,用来存储或读出数据。参数size 是一个整数值,表示要从缓存(buffer)中读出或写入的字符数。
// reading binary file #include <iostream> #include <fstream.h> const char * filename = "test.txt"; int main () { char * buffer; long size; ifstream in (filename, ios::in|ios::binary|ios::ate); size = in.tellg(); in.seekg (0, ios::beg); buffer = new char [size]; in.read (buffer, size); in.close(); cout << "the complete file is in a buffer"; delete[] buffer; return 0; } //运行结果: The complete file is in a buffer
5.缓存和同步(Buffers and Synchronization)
当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起。这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介。例如,对于一个输出流, 每次成员函数put (写一个单个字符)被调用,这个字符不是直接被写入该输出流所对应的物理文件中的,而是首先被插入到该流的缓存(buffer)中。
当缓存被排放出来(flush)时,它里面的所有数据或者被写入物理媒质中(如果是一个输出流的话),或者简单的被抹掉(如果是一个输入流的话)。这个过程称为同步(synchronization),它会在以下任一情况下发生:
当文件被关闭时: 在文件被关闭之前,所有还没有被完全写出或读取的缓存都将被同步。
当缓存buffer 满时:缓存Buffers 有一定的空间限制。当缓存满时,它会被自动同步。
控制符明确指明:当遇到流中某些特定的控制符时,同步会发生。这些控制符包括:flush 和endl。
明确调用函数sync(): 调用成员函数sync() (无参数)可以引发立即同步。这个函数返回一个int 值,等于-1 表示流没有联系的缓存或操作失败。
注:
转载自“https://blog.csdn.net/sinat_36219858/article/details/80369255#17istreamseekg”,感谢!
转载自"https://www.cnblogs.com/zhengfa-af/p/8145443.html",感谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。