# C++流
1、IO流的介绍
IO: 向设备输入数据和输出数据。
C++的IO流
设备:
1) 文件
2) 控制台
3) 特定的数据类型(stringstream)
c++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)
2、读写文件:文件流
1)文件流介绍
文件流: 对文件进行读写操作
头文件: fstream
类库:
ifstream 对文件输入(读文件)
ofstream 对文件输出(写文件)
fstream 对文件输入或输出
2)对文本文件流进行读写
文件打开方式:
| 模式标志 | 描述
| ios::in | 读方式打开文件
| ios:out | 写方式打开文件
| ios::trunc | 如果此文件已经存在, 就会打开文件之前把文件长度截断为0
| ios::app | 尾部最加方式(在尾部写入)
| ios::ate | 文件打开后, 定位到文件尾
| ios::binary | 二进制方式(默认是文本方式)
以上打开方式, 可以使用位操作 | 组合起来
写文本文件
#include<fstream>//对文件进行读写操作
#include<iostream>
#include<string>
using namespace std;
int main() {
string name;
int age;
ofstream outFile;//对文件输出(写文件)
//也可以使用fstream, 但是fstream的默认打开方式不截断文件长度
// ofstream的默认打开方式是, 截断式写入 ios::out | ios::trunc
// fstream的默认打开方式是, 截断式写入 ios::out
// 建议指定打开方式
//指定打开方式,打开文件夹,并且设置打开方式,设置为写入并且清除断尾
outFile.open("lhx.txt", ios::out | ios::trunc);
while (1) {
cout << "请输入姓名:【按Ctrl+z退出】";
cin >> name;
if (cin.eof()) { //判断文件是否介绍
break;
}
outFile << name<<"\t"; //将名字写入文件
cout << "请输入年龄: ";
cin >> age;
outFile << age << endl; //文本文件写入
}
// 关闭打开的文件
outFile.close();
system("pause");
return 0;
}
读文本文件
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
ifstream inFile; //对文件输入(读文件)
string name;
int age;
inFile.open("lhx.txt");//打开文件
while (1) {
inFile >> name; //将文件里的内容输入到name中
if (inFile.eof()) {
break;
}
cout << name << "\t";
inFile >> age;
cout << age << endl;
}
// 关闭打开的文件
inFile.close();
system("pause");
return 0;
}
3)对二进制文件流读写
文本文件和二进制文件的区别是:
文本文件: 写数字1, 实际写入的是 ‘1’。
二进制文件:写数字1, 实际写入的是 整数1(4个字节,最低字节是1, 高3个字节都是0)
写字符‘R’实际输入的还是‘R’。
写二进制文件
使用文件流对象的write方法写入二进制数据.
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
//使用文件流对象的write方法写入二进制数据
int main() {
string name;
int age;
ofstream outFile;
outFile.open("lhx.dat", ios::out | ios::trunc | ios::binary);//binary是二进制的
while (1) {
cout << "请输入名字:【按CTRL+Z结束】";
cin >> name;
if (cin.eof()) {
break;
}
outFile << name << "\t";
cout << "请输入年龄:";
cin >> age;
//outFile << age << endl; //会自动转成文本方式写入
outFile.write((char*)&age, sizeof(age)); //对于不是字符类型的写入要进行转换
}
// 关闭打开的文件
outFile.close();
system("pause");
return 0;
}
读二进制文件
使用文件流对象的read方法.
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main() {
string name;
int age;
ifstream inFile;//对文件进行读取
inFile.open("lhx.dat", ios::in | ios::binary); //文件的打开方式为读取,且用二进制表示
while (1) {
inFile >> name;//将文件的内容读取到name中
if (inFile.eof()) {
break; //判断文件是否读取结束
}
cout << name<<"\t";//输出名字
//跳过中间的制表符
char tmp;
inFile.read((char*)&tmp, sizeof(tmp));
//infile >> age; //从文本文件中读取整数, 使用这个方式
inFile.read((char*)&age, sizeof(age));//将非字符的类型用指针转换为字符然后读入文件
cout << age << endl; //文本文件写入
}
// 关闭打开的文件
inFile.close();
system("pause");
return 0;
}
4)对文件流按指定格式读写取数据
按指定格式写文件
#include<iostream>
#include<fstream>
#include <string>
#include <sstream> //字符串输入输出流
using namespace std;
int main() {
int age;
string name;
ofstream outfile;
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "请输入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判断文件是否结束
break;
}
cout << "请输入年龄: ";
cin >> age;
stringstream s;
s << "name:" << name << "\t\tage:" << age << endl;
outfile << s.str();
}
// 关闭打开的文件
outfile.close();
system("pause");
return 0;
}
按指定格式读文件,C++没有特定办法,只有C语言有,所以就不写了。