这一系列文章的目的是在学习了C++基础后,继续补充一些C++基础和进阶的知识点,包括C++11的相关内容。
以C++11标准为基础。
继承关系
parent | c1 | c2 | c3 | c4 | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ios_base | ios | istream | iostream | fstream | |||||||
stringstream | |||||||||||
ifstream | |||||||||||
istringstream | |||||||||||
ostream | ofstream | ||||||||||
ostringstream | |||||||||||
streambuf | filebuf | ||||||||||
stringbuf |
C++文件读写
文件读写的类
Narrow characters (char) | Wide characters (wchar_t) | |
---|---|---|
ifstream | wifstream | Input file stream class (class ) |
ofstream | wofstream | Output file stream (class ) |
fstream | wfstream | Input/output file stream class (class ) |
filebuf | wfilebuf | File stream buffer (class ) |
- 类ofstream, ifstream 和fstream 是分别继承自ostream, istream 和iostream ,因而像打开文件的模式、读写数据的方式等,都可以使用父类的成员和成员函数。
- 类ofstream, ifstream 和fstream 内部维护一个filebuf对象,作为内部流缓冲区,对关联文件执行输入输出操作。这也对应着ostream, istream 和iostream内部维护的streambuf对象。
开闭文件
类ofstream, ifstream 和fstream 基本一致,以ifstream为例
- 关联与关闭文件
#include <fstream> // 方法一:构造 ifstream(); explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in); explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in); // e.g. std::ifstream ifs ("test.txt", std::ifstream::in); // 方法二:成员函数 open void open (const char* filename, ios_base::openmode mode = ios_base::in); void open (const string& filename, ios_base::openmode mode = ios_base::in); // e.g. std::ifstream ifs; ifs.open ("test.txt", std::ifstream::in); // 关闭文件 void close();
- 文件打开类型
// 注:根据继承关系,此成员变量都能用 std::ifstream::in std::ios_base::in
类型 | 说明 |
---|---|
in | File open for reading,对于ifstream必有 |
out | File open for writing,内部的filebuf也可设置为输出模式 |
binary | 以二进制方式打开 |
app | 从文件尾开始写 |
ate | 从文件尾开始读 |
trunc | 如果文件已存在,则先删除文件 |
- 检查打开情况
bool is_open() const;
读写文件
-
读写文件
// get() // single character int get(); istream& get (char& c); // c-string istream& get (char* s, streamsize n); istream& get (char* s, streamsize n, char delim); // stream buffer istream& get (streambuf& sb); istream& get (streambuf& sb, char delim); // getline() istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); // operator (>>) 不再赘述 // read & write // Extracts n characters from the stream and stores them in the array pointed to by s. istream& read (char* s, streamsize n); // Inserts the first n characters of the array pointed by s into the stream. ostream& write (const char* s, streamsize n);
- 获取和设置流指针
// 获取当前流指针位置 streampos tellg(); // 读操作时,流指针当前位置,返回值是一个整数 streampos tellp(); // 写操作时,流指针当前位置 // 设置流指针位置 istream& seekg (streampos pos); // 给定从文件开始的绝对位置 istream& seekg (streamoff off, ios_base::seekdir way); // 给定从way开始偏移off的位置 ostream& seekp (streampos pos); ostream& seekp (streamoff off, ios_base::seekdir way);
ios_base::seekdir取值 | 说明 |
---|---|
ios_base::beg | beginning of the stream |
ios_base::cur | current position in the stream |
ios_base::end | end of the stream |
- 状态标志函数
// Check whether eofbit is set // 文件读到末尾时,eofbit会置位,对应返回true bool eof() const; // Check whether badbit is set // 如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。 bool bad() const; // Check whether either failbit or badbit is set // 除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。 bool fail() const; // Check whether state of stream is good // 以上三个标志位(eofbit/badbit/failbit)任何一个为true,则goodbit置为0,对应返回false bool good() const;
读写状态标志位 | 意义 | good()是否check | eof()是否check | fail()是否check | bad()是否check | rdstate()是否check |
---|---|---|---|---|---|---|
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 |
读写缓冲与同步
(下次一定… 实际上ifsream和ofstream都会维护一个filebuf对象,其一些成员函数内部都是执行的filebuf的成员函数)
下面是一段摘抄:
当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起。这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介。例如,对于一个输出流, 每次成员函数put (写一个单个字符)被调用,这个字符不是直接被写入该输出流所对应的物理文件中的,而是首先被插入到该流的缓存(buffer)中。
当缓存被排放出来(flush)时,它里面的所有数据或者被写入物理媒质中(如果是一个输出流的话),或者简单的被抹掉(如果是一个输入流的话)。这个过程称为同步(synchronization),它会在以下任一情况下发生:
当文件被关闭时: 在文件被关闭之前,所有还没有被完全写出或读取的缓存都将被同步。
当缓存buffer 满时:缓存Buffers 有一定的空间限制。当缓存满时,它会被自动同步。
控制符明确指明:当遇到流中某些特定的控制符时,同步会发生。这些控制符包括:flush 和endl。
明确调用函数sync(): 调用成员函数sync() (无参数)可以引发立即同步。这个函数返回一个int 值,等于-1 表示流没有联系的缓存或操作失败。
————————————————
版权声明:本文为CSDN博主「追求执着」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kingstar158/article/details/6859379
C语言写入文件
#include “stdio.h"
int main()
{
FILE *fp;
fp=fopen("2.txt","w"); // 以写入的方式,新建或打开文件,并在文件末尾添加内容
int n=1;
fprintf(fp,"%d",n);
fclose(fp);
return 0;
}