文件模式
-
文件模式与条件状态标志一样,文件模式也是整形常量,用位操作符设置一个或多个模式。
文件流构造函数和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的成员函数。
- 可对string对象进行每行的输入
stringstream特定的操作 | |
---|---|
stringstream strm; | 创建自由的stringstream对象 |
stingstream strm(s) | 创建存储s的副本stringstream对象,其中s是string类型的对象 |
strm.str() | 返回strm中存储的string类型对象 |
strm.str(s) | 将string类型的s复制给strm,返回void类型 |
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 = 1024;
ostringstream 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。