使用ostream,istream通用输入输出流时,通常情况下不用创建对象,可以直接使用,文本主要针对需要创建对象的文件输入输出流和字符串输入输出流
1.文件输出流ofstream
包含文件#include<fstream>
①open函数
用途:把该输出流与一个特定磁盘文件关联起来
语法:
ofstream file("filename",打开模式);
或
ofstream file;
file.open("filename",打开模式);
打开模式:
可以用按位或符号"|"组合多种模式
模式标志 | 描述 |
---|---|
ios_base::app | 追加模式。所有写入都追加到文件末尾。 |
ios_base::ate | 文件打开后定位到文件末尾。 |
ios_base::in | 打开文件用于读取。(可以避免删除文件现存内容) |
ios_base::out | 打开文件用于写入。(默认) |
ios_base::trunc | 如果该文件已经存在,删除原有内容(默认) |
ios_base::binary | 以二进制模式打开一个文件(默认文本模式) |
②close函数
语法:
file.close()
③write函数
用途:把一个内存中的一块内容写到一个文件输出流中
格式:一个char指针(指向内存数据的其实地址)+ 字节数
// 文件输出流
#include<fstream>
using namespace std;
struct Date {
int monday, day, year;
};
int main() {
Date dt = { 6,10,92 };
// 以二进制形式打开
ofstream file("文件路径", ios_base::binary);
// 显示转换该对象地址的类型
file.write(reinterpret_cast<char*>(&dt), sizeof(dt));
file.close();
return 0;
}
④put函数
用途:把字符写到输出流中
cout.put('A');
相当于cout<<'A';
⑤seekp和tellp函数
用途:一个文件输出流保存一个内部指针指向下一次写数据的位置
seekp设置这个指针,tellp返回该文件位置指针值
2.字符串输出流ostringstream
包含文件#include<sstream>
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
template<class T>
// 该函数模板可以把任何支持<<插入符的对象转换为字符串
inline string tostring(const T& v) {
ostringstream os;
// 创造字符串输出流
os << v;
// 将变量v的值写入字符串流
return os.str();
// 返回输出流生成的字符串
}
int main() {
string str1 = tostring(5);
cout << str1 << endl;
string str2 = tostring(1.2);
cout << str2 << endl;
string str3 = tostring("Hello World");
cout << str3 << endl;
return 0;
}
3.文件输入流ifstream
包含文件:#include<fstream>
构造输入流对象->用文件流对象从文件读取数据
①.open构造
ifstream myfile;
myfile.open("filename");
提取运算符>>不能读入空格
ios_base::in | 打开文件用于读取。(默认) |
ios_base::binary | 以二进制模式打开一个文件(默认文本模式) |
②close函数(与输出流一样)
用于手动关闭打开的文件
③get函数
与提取运算符>>很相像,但是可以读取空白字符
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
char ch;
while ((ch = cin.get()) != EOF)
cout.put(ch);
return 0;
}
④getline函数
非成员函数:包含#include<string>
//从键盘读入
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
string line;
// 可以指定终止字符
// (一定要有终止字符,不指定则默认换行符,终止字符不读入)
// cin表示从键盘读入
getline(cin, line,'t');
cout << line << endl;
return 0;
}
// 输入
people today
// 输出
people
//从文件读入
#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
using namespace std;
int main() {
//输出流输入文件内容
string line;
getline(cin, line);
ofstream file1("filename",ios_base::out);
file1.write(reinterpret_cast<char*>(&line), sizeof(line));
file1 << line << endl;
file1.close();
//输入流读取文件内容
string line2;
ifstream file2("filename", ios_base::in);
getline(file2, line2);
cout << line2 << endl;
return 0;
}
⑤read函数
// 文件输出流(一个结构体)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Date {
int monday, day, year;
};
int main() {
//读入
Date dt = { 6,10,92 };
// 以二进制形式打开
ofstream file("filename",ios_base::binary);
// 显示转换该对象地址的类型
file.write(reinterpret_cast<char*>(&dt), sizeof(dt));
file.close();
//输出
//以二进制打开
ifstream is("filename", ios_base::in | ios_base::binary);
if ( is ) {
Date dt2;
is.read(reinterpret_cast<char*>(&dt2), sizeof(dt2));
cout << dt2.monday << " " << dt2.day << " " << dt2.year << endl;
}
//添加打开错误提示
else {
cout << "ERROR to open file" << endl;
}
return 0;
}
⑥seekg.tellg函数
// seekg.tellg
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int values[] = { 3,7,0,5,4 };
//建立一个输出流对象
ofstream os("filename", ios_base::out | ios_base::binary);
//写入内容
os.write(reinterpret_cast<char*>(&values), sizeof(values));
os.close();
//建立一个输入流
//输出指定位置
ifstream is("filename", ios_base::in | ios_base::binary);
if (is) {
// 移动指针位置(移动三个到达第四个)
is.seekg(3 * sizeof(int));
int v;
is.read(reinterpret_cast<char*>(&v), sizeof(int));
cout << "The 4th integer in the file is: " << v << endl;
}
is.close();
//建立输入流对象
//找出其中0元素的位置
is.open("filename", ios_base::in | ios_base::binary);
//打开时默认seekg,tellg在最开始的位置
if (is) {
is.seekg(1*sizeof(int));
//seekg为tellg的起始位置
while (is) {
//每一次移动的是一个int单位
//tellg的返回值是streampos类型,seekg不是,seekg用于移动
streampos here = is.tellg();
cout << here << endl;
int v2;
is.read(reinterpret_cast<char*>(&v2), sizeof(v2));
if (is && v2 == 0) {
cout << "Postition " << here << " is 0 " << endl;
}
//不用手动移动tellg指针
}
}
is.close();
return 0;
}
//输出
/*
The 4th integer in the file is : 5
4
8
Postition 8 is 0
12
16
20
*/
4.字符串输入流
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
template<class T>
inline T fromString(const string& str) {
istringstream is(str);
// 创建字符串输入流
T v;
is >> v;
// 从字符串输入流中读取变量v
return v;
// 返回变量v
}
int main() {
// 显示转换类型
int v1 = fromString<int>("5");
cout << v1 << endl;
double v2 = fromString<double>("1.2");
cout << v2 << endl;
string v3 = fromString<string>("Hello World");
cout << v3 << endl;
// 只能输出Hello(不能读取空格)
return 0;
}