IO对象不可以赋值或拷贝-8.1.1
IO流的条件状态8.1.2
控制输出流的缓存–8.1.3
文件输入与输出
- 文件被打开后不能再用其它对象来进行打开与操作
- 文件模式
文件模式 | |
---|---|
in | 以读方式打开 |
out | 以写方式打开 |
app | 每次写操作前均定位到文件末尾 |
ate | 打开文件后立即定位到文件末尾 |
trunc | 截断文件? |
binary | 以二进制方式进行IO? |
注意: 以out模式打开文件会丢弃已有数据(由于隐含地打开了trunc模式),保存的唯一方式是显示指定app或者in模式
这样才能运行多次后有多次个输入的数据
int main()
{
fstream a("text.txt",ofstream::out | ofstream::app);
a<<"dwadaffff\ndwadawda\nffff\nff";
}
string Streams
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
bool valid(const string);
struct PersonInfo
{
string name;
vector<string>phones;
};
int main()
{
ifstream in_file("text.txt");
string line, word;
vector<PersonInfo> people;
if (in_file)
{
PersonInfo temp_info;
while (getline(in_file, line))
{
istringstream record(line);
record >> temp_info.name;
while (record >> word)
{
temp_info.phones.push_back(word);
}
people.push_back(temp_info);
}
}
else
{
cout << "open file fail" << endl;
}
return 0;
}