Cpp Primer<<学习IO标准库--文件模式、字符串流_7

本文介绍了C++中的文件模式,强调在追加已有数据到文件时需使用app模式。此外,详细阐述了字符串流,包括istringstream用于读取字符串,ostringstream用于写入字符串,以及stringstream的读写功能。sstream头文件提供了相关类的定义,并允许直接操作string对象。通过istringstream,可以从字符串中提取算术值。
摘要由CSDN通过智能技术生成

文件模式

文件模式与条件状态标志一样,文件模式也是整形常量,用位操作符设置一个或多个模式。
文件流构造函数和open函数都提供了默认实参设置文件模式。

文件模式
in打开文件读操作
out打开文件读操作
app在每次写之前找到文件尾
ate打开文件后立即将文件定位在文件尾
trunc打开文件时清空已存在的文件流
binary以二进制模式进行IO操作
out、trunc和app模式只能用于指定与ofstream或fstream对象关联的文件。
in模式只能哟哦那个与指定与ifstream或fstream对象相关联的文件。所有的文件都可以用ate或binary模式代开。ate模式只在打开时有效,文件打开后立即定位在文件尾。以binary模式打开的流则将文件以字节序列的形式处理,而不解释流中的字符。

对于保存数据到已存在数据的文件中,唯一方法是显示指定使用app模式打开时文件:

// out mode by default;truncates file named “file1”
ofstream outfile(“file1”);
//equilvalent  effect:”file1” is explicitly truncated
ofstream outfile2(“file1”,ofstream::out | ofstream::trunc );
// append mode; adds new data at end of existing file named “file2”
ofstream appfile(“file2”,ofstream::app);
2.1对同一个文件输入和输出运算
fstream对象既可以读也可以写与它相关联的文件。fstream使用它的文集爱你取决于对文件使用文件模式。默认情况下,fstream对象以in和out模式同时打开文件,此时被打开的文件不清空。若以trunc模式打开文件,文件都会被强制清空。
// open for and out
fstream inOut(“copyOut”,stream::in | fstream::out);
2.2模式是文件的属性而不是流的属性
每次打开文件时都会设置模式:
ofstream outfile;
//  output mode set to out,”scratchpad” truncated 
outfile.open(“scratchpad”,ostream::out);
outfile.close();     //  close outfile so we can rebind it
//  appends to file named “previous”
outfile.open(“previous”,ostream::app);
outfile.close();
//  output mode set by default,”out” truncated
outfile.open(“out”);

只要调用open函数,就要设置文件模式,其模式的设置可以是显示的也可以是隐式的,隐式设置则是使用默认的,即同时使用in和out模式打开文件。

2.3打开模式的有效组合
文件模式的组合
out打开文件做写操作,删除文件中已存在的数据
out │ app打开文件做写操作,在文件尾写入
out│trunc与out模式相同
in打开文件做读操作
in │out打开文件做读、写操作,并定位于文件开头处
in │ out │ trunc打开文件做读、写操作,删除文件中已有的数据

2.4一个打开并检查输入文件的程序

//opens in binding it to givenfile
ifstream& open_file()
{
    in.close();      //  close in case it was already open
    in.clear();      //  clear any existing errors
    // if the open fails,the stream will be in an invlide state
    in.open(file.c_str());     //  open the file we were given
    return in;     //  condition state is good if open succeeded
}

字符串流

标准库定义三种类型的字符串流:
istringstream类,由istream生而来,提供读string的功能
ostreamstring类,由ostream类型派生而来,提供写string的功能
streamstring类,由iostring类型,派生而来,提供读写string的功能
sstream头文件包含上述三个类的定义, iostream上的操作同样适用于sstream中的类型。sstream还定义了一个有string类型的普通形参的构造函数,该函数将string类型的实参复制给stringstream对象。当然也还定义了名为str的数据成员,用来读取或设置stringstream对象所操纵的string值。ssteam头文件中不包含有像fstream的open和close的成员函数。但是定义一个str的成员函数。

stringstream特定的操作
stringstream strm;创建自由的stringstream对象
stingstream strm(s)创建存储s的副本stringstream对象,其中s是string类型的对象
strm.str()返回strm中存储的string类型对象
strm.str(s)将string类型的s复制给strm,返回void类型
1.stringstream对象的使用
可对string对象进行每行的输入
string line,word;     // will hold a line and word from input,respectively
while (getline(cin,line)) {    // read a line from the input into line
    // do per-line processing
istringstream stream(line);   // bind to stream to the line we read
while (stream >> word) {  //  read a word from line
    // do per-word processing 
    }
}
2.stringstream提供的转换和/或格式化
stringstream 对象的一个常见用法是,需要在多种数据类型之间实现自动格式化是使用该类型。
int val1 = 512,val2 = 1024ostringstream format_message;
//  ok:converts values to a string representation
format_message << “val1:” << val1 << “val2: ” << val2 << “\n”;  // format的值为val1:512val2:1024

用istringstream类型的对象读取string对象,可将字符串中用字符表示的数据转换为对应的算术值。

//  str member obtains the string associated with a stringstream
istringstream input_istring(format_message.str());
string dump;      // place to dump the labels from the formatted message
extracts the stored ascii values,converting back to arithmetic types
input.istring >> dump >> val1 >> dump >> val2;
cout << val1 << “ “ << val2 << endl; // prints 512 1024

貌似书中有个不足之处,就是书中的这段代码

format_message <<"val1:" << val1 << "\n" << "val2:" << val2 << "\n";

应该改为

format_message <<"val1:" << val1 << "val2:" << val2 << "\n";

这样子,在下面的恢复算术值的时候,val1才能够恢复回原来的512。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取 代< fstream.h>,所有的C++标准头文件都是无后缀的。)。这是 <iostream.h>的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, <iostream.h> 已经被<fstream.h>包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作类的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个类对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了操作,对 "<<" 和">>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 很像. 和刚刚讨论的输出很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往里插入变量和符号吧?好,?在 "<<" (插入)?操作 符之后,是">>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值