1、iostream 数据流输入输出
■ \blacksquare ■ iostream是一个头文件,意思是输入输出流,in(输入)、out(输出)、stream(流)。
■ \blacksquare ■ cin - 写数据
∙ \bullet ∙ cin表示标准输入(standard input)的istream类对象,可以从设备读出数据。
■ \blacksquare ■ cout - 读数据
∙ \bullet ∙ cout表示标准输出(standard output)的ostream类对象,可以向设备输出或者写数据。
■ \blacksquare ■ cerr - 报错误
∙ \bullet ∙ cerr表示标准错误(standard error)的ostream类对象。cerr是导出程序错误消息的地方,它只能允许
向屏幕设备写数据。
■ \blacksquare ■ cout 和 cerr 区别
∙ \bullet ∙ cout对应于标准输出流,默认情况下是显示器。这是一个被缓冲的输出,可以被重定向。cerr对应标准错误流,用于显示错误消息。默认情况下被关联到标准输出流,但它不被缓冲,可以直接发送到显示器,而无需等到缓冲区或者新的换行符时,才被显示,一般情况下不被重定向。
■ \blacksquare ■ 代码示例
#include<iostream>
using namespace std;
int main(){
//整型变量输入
int a;
std::cout << "Please enter an integer variable:" << std::endl;
std::cin >> a;
std::cout << a << std::endl;
//浮点型变量输入
double d;
std::cout << "Please enter a floating variable:" << std::endl;
std::cin >> d;
std::cout << d << std::endl;
//字符型变量输入
char ch;
std::cout << "Please enter a character variable:" << std::endl;
std::cin >> ch;
std::cout << ch << std::endl;
//字符串型变量输入
string str;
std::cout << "Please enter a string variable:" << std::endl;
std::cin >> str;
std::cout << str << std::endl;
//布尔类型变量输入
bool flag;
std::cout << "Please enter a Boolean variable" << std::endl;
std::cin >> flag;
std::cout << flag << std::endl;
//错误信息打印
std::cerr << "There is no errors!" << std::endl;
return 0;
}
2、fstream文件输入输出
■ \blacksquare ■ 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化。
■ \blacksquare ■ 文件操作头文件:fstream
■ \blacksquare ■ 文件类型分为两种:
∙ \bullet ∙ 文本文件:以 ASCII码形式存储在计算机中。
∙ \bullet ∙ 二进制文件:以二进制形式存储在计算机中 。
■ \blacksquare ■ 操作文件的三位类:
∙ \bullet ∙ ofstream:写操作
∙ \bullet ∙ ifstream:读操作
∙ \bullet ∙ fstream:读写操作
3、文本文件
■ \blacksquare ■ 写文件步骤:
∙ \bullet ∙ 包含头文件:#include
∙ \bullet ∙ 创建流对象:ofsteam ofs;
∙ \bullet ∙ 打开文件:ofs.open(“文件路径”,打开方式);
∙ \bullet ∙ 写数据:ofs <<“写入数据”;
∙ \bullet ∙ 关闭文件:ofs.close();
■ \blacksquare ■ 文件打开方式
打开方式 | 解释 |
---|---|
ios::in | 为读文件而打开文件 |
ios::out | 为写文件而打开文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 追加方式写文件 |
ios::trunc | 如果文件存在先删除,再创建 |
ios::binary | 二进制方式 |
备注:可以多种方式配合使用,利用 | 操作符
例如:用二进制方式写文件 ios::binary | ios::out
■ \blacksquare ■ 代码示例
#include<fstream>
void test01()
{
std::ofstream ofs;
ofs.open("text.txt",std::ios::out);
ofs << "name:zhangsan" << std::endl;
ofs << "gender:man" << std::endl;
ofs << "age:18" << std::endl;
ofs.close();
}
int main(){
test01();
return 0;
}
■ \blacksquare ■ 运行结果
自动生成一个 text.txt 文件,并且将内容写入到 txt 里。
■ \blacksquare ■ 总结
∙ \bullet ∙ 文件操作必须包含头文件 fstream
∙ \bullet ∙ 写文件可以利用 ofstream 或者 fstream 类
∙ \bullet ∙ 打开文件时候需要指定操作文件的路径以及打开方式
∙ \bullet ∙ 利用 << 可以向文件中写数据
∙ \bullet ∙ 操作完毕,要关闭文件,以免损坏文件
■ \blacksquare ■ 读文件
■ \blacksquare ■ 读文件步骤
∙ \bullet ∙ 包含头文件 :#include
∙ \bullet ∙ 创建流对象 :ifstream ifs;
∙ \bullet ∙ 打开文件并判断文件是否打开成功:ifs.open(“文件路径”,打开方式); if (!ifs.is_open());
∙ \bullet ∙ 读数据:四种方式读取
∙ \bullet ∙ 关闭文件:ifs.close();
■ \blacksquare ■ 代码示例
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void test02()
{
std::ifstream ifs;
ifs.open("text.txt",ios::in);
//判断文件是否能打开
if(!ifs.is_open())
{
cout << "Files cannot be opened" << std::endl;
}
//方式1:遇到空格就停止读取
std::cout << "########## Method 1 ##########" << std::endl;
char buf1[1000] = {0};
while (ifs >> buf1)
{
std::cout << buf1 << std::endl;
//break; //加上break即可观察到此方式遇到空格就会停止读取
}
//方式2:遇到换行就停止读取
std::cout << "########## Method 2 ##########" << std::endl;
char buf2[1024] = {0};
while(ifs.getline(buf2,sizeof(buf2)))
{
std::cout << buf2 << std::endl;
//break; //加上break即可观察到此方式遇到换行就会停止读取
}
//方式3:动态添加内容,不用担心存不下
std::cout << "########## Method 3 ##########" << std::endl;
string buf3;
while(getline(ifs,buf3))
{
std::cout << buf3 << std::endl;
}
//方式4:逐字符读取
std::cout << "########## Method 4 ##########" << std::endl;
char c;
while ((c = ifs.get()) != EOF) //EOF:End Of File
{
std::cout << c;
}
ifs.close();
}
int main() {
test02();
return 0;
}
■ \blacksquare ■ 总结
∙ \bullet ∙ 读文件可以利用 ifstream 或者fstream类
∙ \bullet ∙ 利用 is_open() 函数可以判断文件是否打开成功
∙ \bullet ∙ close() 关闭文件
4、二进制文件
■ \blacksquare ■ 以二进制的方式堆文件进行读写操作,需要指定方式为: ios::binary
■ \blacksquare ■ 写文件
∙ \bullet ∙ 二进制方式写文件主要利用流对象调用成员函数write
∙ \bullet ∙ 函数原型 : ostream& write(const char * buffer, int len);
∙ \bullet ∙ 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数。
■ \blacksquare ■ 代码示例
#include<fstream>
#include<string>
using namespace std;
//二进制文件 写文件
void test03(){
//创建输出流对象并打开
ofstream ofs("test.txt",ios::out | ios::binary);
char str[] = "I like study C++!";
//写入文件 这里与文本写入方式不一样
ofs.write(str,sizeof(str));
//关闭文件
ofs.close();
}
int main(){
test03();
return 0;
}
■
\blacksquare
■ 运行结果
■ \blacksquare ■ 总结
∙ \bullet ∙ 文件输出流对象可以通过write函数,以二进制方式写数据
■ \blacksquare ■ 读文件
∙ \bullet ∙ 二进制方式读文件主要利用流对象调用成员函数 read
∙ \bullet ∙ 函数原型: istream& read(char *buffer,int len);
∙ \bullet ∙ 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数。
■ \blacksquare ■ 代码示例
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
//二进制文件 读文件
void tesr04(){
//创建输出流对象并打开
ifstream ifs("test.txt",ios::in | ios::binary);
//也可以先创建对象再打开文件
// ifstream ifs;
// ifs.open("test.txt",ios::in | ios::binary);
//读取文件
//获取字节流的长度
ifs.seekg(0,std::ios::end);
int len = ifs.tellg();
ifs.seekg(0,std::ios::beg);
//创建一段存储空间存储数据
char* buff = new char[len];
//读取数据
ifs.read(buff,len);
for(int i = 0; i< len; i++){
cout << buff[i];
}
cout << endl;
delete[]buff;
//关闭文件
ifs.close();
}
int main(){
tesr04();
return 0;
}
■
\blacksquare
■ 运行结果
■ \blacksquare ■ 总结
∙ \bullet ∙ 文件输入流对象可以通过read函数,以二进制方式读数据
附代码链接:https://www.aliyundrive.com/s/MMne62rm3dC