C++标准库
一些标准库的使用方法
ofstream
#include<fstream>
ofstream temp(Filepath.txt);
temp<<………;
temp.close();
精度设置
std::ofstream(const char* filepath);
// 不适用科学计数法表示
ofs<<std::fixed
// 有效数字位数
ofs.precision(int num);
// 当输出一行数字的精度不同时, 每次修改精度前单独设置一次
ofs<<num<<"\t";
ofs.precision(5);
ofs<<iter.incidence<<"\t";
ofs.precision(2);
ofs<<iter.row<<"\t"<<iter.col<<"\t";
ofs.precision(5);
ofs<<iter.point.x<<"\t"<<iter.point.y<<"\t"<<iter.point.z<<"\n";
// ...
ofs.close();
占位符 & 输出位置
ofs<<std::right;
ofs<<std::setw(4)<<1<<std::setw(4)<<10<<std::setw(4)<<100<<std::endl;
ofs.close();
/// 输出结果格式如下
/// “___1__10_100”(将空格替换为下划线方便查看间距)
ifstream
ifstream ifs;
ifs.open(txtsrc);
if (!ifs.is_open()){
return false;
}
string str;
string Separator = string(",");
while (getline(ifs, str))
{
/// str...
}
ifs.close();
二进制文件读取及跳字节
场景:2022.3.17 其他软件无法正常显示.dat
std::ifstream ifs("filepath",ifsteam::binary);
if(!ifs.is_open()){
return false;
}
double value;
while(ifs.read((char*)value,sizeof(double)/*8*/))
{
//如果value异常 (error只是一个假想的函数)
if(error(value)){
//ifs指针从当前位置(ios::cur)向前跳n-1的字节,
ifs.seekg(1-sizeof(double),ios::cur);
continue;
}
}
std::ifstream ifs("filepath",ifsteam::binary),用二进制方式读取文件。
ifs.read(char_type* ,streamsize ),每次读取streamsize长度的字节数,并存储到指定位置(char_type*)。
seekg:
ifs.seekg(off_type,ios_base::seekDir) ifs的当前指针移动到seekDir处并偏移off_type字节。
ios_base::seekDir:
ios::beg 表示输入流的开始位置
ios::cur 表示输入流的当前位置
ios::end 表示输入流的结束位置
filesystem
c++17, 类似于QFileInfo
#include <filesystem>
namespace fs = std::filesystem;
std::string root_path;
fs::path fs_path(root_path);
bool ans;
std::string str_path = fs_path.string(); //路径转字符串
ans = fs_path.is_directory(); //文件夹
ans = fs_path.is_regular_file(); //文件
fs::path fs_parentpath = fs_path.parent_path(); //文件的路径
fs::path filename = fs_path.filename(); //文件名
fs::path extension = fs_path.extension(); //拓展名(后缀)
for (auto& iter : directory_iterator(root_path)){
//查看当前路径下的所有文件/文件夹
}
for (auto& iter : recursive_directory_iterator(root_path)){
//迭代查看当前路径及子文件内的所有文件/文件夹
}
chrono
计时功能
auto start = std::chrono::system_clock::now();
func();/// 一个费时的函数
auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
cout << "Spent" << double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << " seconds." << endl;
C++11的 #include<chrono> 通过模板调整精度可以达到微妙级,相比传统的#include <ctime> 精度更高。
chrono::time_point -> string
auto tp = std::chrono::system_clock::now(); // time_point
time_t time = std::chrono::system_clock::to_time_t(tp);
std::stringstream ss;
ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S");
std::string str = ss.str();
首先通过to_time_t将time_point转换为time_t,然后使用std::localtime()将time_t转换为tm,再使用put_time()将tm转换为指定输出格式的_TimeObj,存储到stringstream中,最后通过stringstream::str()转换为string。
如果使用tm存储时间数据,则可以直接使用put_time将时间转换为字符串。
put_time & get_time
按照指定格式输出和获取时间
put_time的链接中包含了支持的各种格式说明,同样适用于get_time
例如:
| 说明符 | 替换 | 示例 |
|---|---|---|
| %y | 年份,最后两位数字(00-99) | 01 |
| %Y | 年份,四位数字 | 2001 |
| %m | 月份的十进制数(01-12) | 08 |
| %M | 分钟(00-59) | 56 |
| %d | 月份中的某一天,用零填充(01-31) | 23 |
| %D | MM/DD/YY日期的简写,相当于%m/%d/%y | 08/23/19 |
| %F | YYYY-MM-DD日期的简写,相当于%Y-%m-%d | 2023-04-14 |
| ... | ... | ... |
示例:
std::tm t = {};
std::istringstream ss("2011-Februar-18 23:12:34");
ss.imbue(std::locale("de_DE.utf-8"));
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail())
std::cout << "Parse failed\n";
else
std::cout << std::put_time(&t, "%c") << '\n';
copy from CMake Cookbook 3.7章节
// copy from CMake Cookbook 3.7章节
std::chrono::time_point<std::chrono::system_clock> start, end;
std::chrono::duration<double> elapsed_seconds;
std::time_t end_time;
start = std::chrono::system_clock::now();
// ...
end = std::chrono::system_clock::now();
// Report times
elapsed_seconds = end - start;
end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"matrices allocated and initialized"
<<std::put_time(std::localtime(&end_time),"%a %b %d %Y %r\n")
<<"elapsed time: "
<<elapsed_seconds.count()<<"s\n";
// cmd打印内容
// matrices allocated and initialized Sun Jun 17 2018 11:04:20 AM
// elapsed time: 0.0492328s
popen
可调用cmd,并且获取cmd实时的(每一行)输出结果,代码与控制台的交互。
示例:
通过调用cmd,执行pip list命令,该命令是查看pip包管理工具中已安装的依赖库
char buffer[1024];
const char* szCmd = "pip";
FILE* pipe = _popen(szCmd, "r");
if (pipe != nullptr) {
while (fgets(buffer, 1024, pipe) != NULL){
std::cout << std::string(buffer);
}
fclose(pipe);
}