sstream之安全输入输出

文章目录

    • 1、sstream库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。注意,使用string对象来代替字符数组。
    • 2、日志文件

1、sstream库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。注意,使用string对象来代替字符数组。

1、**类型安全**:自动使用正确格式。【传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险】
2、**缓冲区安全**:自动存储分配空间,不会溢出。【传入参数和目标对象的类型被自动推导出来,自动分配存储空间】

2、日志文件

_CRT_SECURE_NO_WARNINGS

#include 
#include 
#include 
#include 
#include 

int main() {
	char filename[1024];
	time_t t = time(NULL);
	strftime(filename, sizeof(filename) - 1, "%Y-%m-%d-%H-%M-%Slog.txt", localtime(&t));
	std::ofstream of(filename, std::ios::app);
	std::stringstream ss;
	ss << "HelloWorld" <<std::endl;
	int face_num = 10;
	ss << "检测到的人脸数:" << face_num << std::endl;	
	of << ss.str();
	
	of.close();
	return 1;
}

重复使用需要清理

ss.clear(); //多次使用需要

你可能感兴趣的:(c++编程)