当前位置: 首页 > news >正文

长沙环路建设开发有限公司网站wordpress幻灯片

长沙环路建设开发有限公司网站,wordpress幻灯片,江苏建设厅执业网站,诺盾网站建设目录 路径操作 目录遍历 文件检查和操作 总结 每次写C进行目录操作时#xff0c;我一般都是调平台的SDK#xff0c;尤其是win32 api 非常难记#xff0c;于是查一下文档看看有没有和Python中os模块一样好用的库。 于是发现 filesystem#xff0c;从来没用过#xff0…目录 路径操作 目录遍历 文件检查和操作 总结 每次写C进行目录操作时我一般都是调平台的SDK尤其是win32 api 非常难记于是查一下文档看看有没有和Python中os模块一样好用的库。 于是发现 filesystem从来没用过我的第六版C primer 最新标准只介绍了C11 用法整理如下仅供参考 当你需要对计算机上的文件和目录进行操作时C17标准库中的filesystem头文件可以为你提供方便的工具。它提供了一系列的类和函数来处理文件和目录的操作包括路径操作、目录遍历、文件检查、文件操作等等。本文将为你介绍filesystem头文件的使用方法和功能。 当你需要对计算机上的文件和目录进行操作时C17标准库中的filesystem头文件可以为你提供方便的工具。它提供了一系列的类和函数来处理文件和目录的操作包括路径操作、目录遍历、文件检查、文件操作等等。本文将为你介绍filesystem头文件的使用方法和功能。 路径操作 filesystem头文件中的path类用于表示文件或目录的路径。你可以使用各种运算符和函数来操作路径。 首先你可以使用/运算符来将路径组合起来。例如 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p1 C:/Users/;fs::path p2 John/Documents/;fs::path p3 p1 / p2;std::cout p3 std::endl;return 0; }上面的代码将输出C:/Users/John/Documents这是将两个路径组合起来的结果。 你还可以使用运算符将路径附加到现有路径上 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p1 C:/Users/;fs::path p2 John/Documents/;p1 p2;std::cout p1 std::endl;return 0; }上面的代码将输出C:/Users/John/Documents这是将路径附加到现有路径的结果。 如果你需要比较两个路径你可以使用和!运算符来比较它们是否相等 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p1 C:/Users/John/Documents/;fs::path p2 C:/Users/Jane/Documents/;if (p1 p2) {std::cout The paths are equal std::endl;} else {std::cout The paths are not equal std::endl;}return 0; }上面的代码将输出The paths are not equal这是因为两个路径不相等。 除了运算符path类还提供了一些有用的函数来操作路径。例如你可以使用parent_path()函数来获取路径中的父目录 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents/report.txt;fs::path parent p.parent_path();std::cout parent std::endl;return 0; }上面的代码将输出C:/Users/John/Documents这是路径中的父目录。 你还可以使用stem()函数来获取路径中的文件名不包括扩展名 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents/report.txt; fs::path filename p.stem(); std::cout filename std::endl; return 0; } 上面的代码将输出report这是文件名不包括扩展名。 如果你需要获取路径中的扩展名可以使用extension()函数 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents/report.txt;fs::path extension p.extension();std::cout extension std::endl;return 0; } 上面的代码将输出.txt这是文件的扩展名。 目录遍历 filesystem头文件提供了一些函数来遍历目录中的文件和子目录。你可以使用directory_iterator类来遍历目录中的所有内容。例如下面的代码将遍历目录C:/Users/John/Documents中的所有文件和子目录 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents;for (auto entry : fs::directory_iterator(p)) {std::cout entry.path() std::endl;}return 0; }上面的代码将输出目录中的所有文件和子目录的路径。 如果你只想遍历目录中的文件可以使用is_regular_file()函数来判断一个条目是否是一个普通文件 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents;for (auto entry : fs::directory_iterator(p)) {if (fs::is_regular_file(entry)) {std::cout entry.path() std::endl;}}return 0; }上面的代码将输出目录中的所有文件的路径。 如果你只想遍历目录中的子目录可以使用is_directory()函数来判断一个条目是否是一个目录 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents;for (auto entry : fs::directory_iterator(p)) {if (fs::is_directory(entry)) {std::cout entry.path() std::endl;}}return 0; }上面的代码将输出目录中的所有子目录的路径。 C17 的 filesystem 标准库提供了获取当前工作目录和创建目录的功能。 获取当前工作目录的方式是通过 std::filesystem::current_path 函数来实现它返回一个 std::filesystem::path 类型的对象表示当前工作目录的路径。例如 #include filesystem #include iostreamint main() {std::filesystem::path currentPath std::filesystem::current_path();std::cout Current working directory is: currentPath std::endl;return 0; }创建目录的方式是通过 std::filesystem::create_directory 函数来实现它接受一个 std::filesystem::path 类型的对象表示要创建的目录的路径。例如 #include filesystem #include iostreamint main() {std::filesystem::path dirPath C:/mydir;if (std::filesystem::create_directory(dirPath)) {std::cout Directory created successfully. std::endl;} else {std::cout Failed to create directory. std::endl;}return 0; }文件检查和操作 filesystem头文件中的fstream和iostream头文件提供了一些函数来检查和操作文件。例如你可以使用exists()函数来检查文件是否存在 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents/report.txt;if (fs::exists(p)) {std::cout The file exists std::endl;} else {std::cout The file does not exist std::endl;}return 0; }上面的代码将输出The file exists如果文件存在的话。 你可以使用remove()函数来删除文件 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path p C:/Users/John/Documents/report.txt;if (fs::exists(p)) {fs::remove(p);std::cout The file was successfully deleted std::endl;} else {std::cout The file does not exist std::endl;}return 0; }上面的代码将删除文件并输出The file was successfully deleted如果文件存在的话。 你也可以使用copy()函数来复制文件 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path source C:/Users/John/Documents/report.txt;fs::path target C:/Users/John/Documents/report_copy.txt;if (fs::exists(source)) {fs::copy(source, target);std::cout The file was successfully copied std::endl;} else {std::cout The file does not exist std::endl;}return 0; }上面的代码将复制文件并输出The file was successfully copied如果源文件存在的话。 你还可以使用rename()函数来重命名文件 #include filesystem #include iostreamnamespace fs std::filesystem;int main() {fs::path old_path C:/Users/John/Documents/report.txt;fs::path new_path C:/Users/John/Documents/report_renamed.txt;if (fs::exists(old_path)) {fs::rename(old_path, new_path);std::cout The file was successfully renamed std::endl;} else {std::cout The file does not exist std::endl;}return 0; }上面的代码将重命名文件并输出The file was successfully renamed如果原文件存在的话。 总结 filesystem头文件提供了一组强大的工具来处理文件系统。你可以使用它来管理文件和目录获取文件和目录的信息以及执行文件操作。这些功能使得处理文件和目录变得更加容易而且可移植性更好因为filesystem头文件可以在多个平台上使用。
http://www.hkea.cn/news/14426549/

相关文章:

  • seo网站诊断html网站开场动画效果模板
  • 域名解析要登入哪个网站做北京网页制作培训班
  • 免费网站建站百度云网络文化经营许可证全国有多少张
  • 网站开发维护印花税厦门网站快速排名优化
  • 呢图网站场建设封面网站建设找业主签字模板
  • 阿里云1m服务器可以搭建网站wordpress 筛选
  • 跨境商城网站制作有了域名如何做网站
  • 网站建设结构设计哈尔滨百度网站排名
  • 安徽商会网站建设方案设计网站100个免费
  • php网站建设设计制作方案知名男艺人工作室
  • 无锡做网站公司在哪里flash工作室网站模板
  • 企业宣传网站建设需求说明书的模板好看网电影网站模板免费下载
  • 心理网站建设策划书制作游戏的软件手机版
  • 设计软件免费下载官方网站廊坊网站建设墨子
  • python爬虫做网站制作图片的免费软件
  • 厦门建设局网站改到哪网络公司经营范围可以加技术培训
  • 团购网站大全讯美深圳网站建设
  • wordpress网站合并淮南网云小镇最新消息
  • 鞍山网站开发公司wordpress h标签
  • 一站式网站建设费用个人网页图标设计
  • 建设部网站查资质铭泰东莞网站建设
  • 郑州网站营销推广中国五大门户网站
  • 文化传媒网站封面免费的行情软件
  • 网站制作 南宁焦作网站设计多少钱
  • 做网站的销售工作好吗衡水微信网站建设
  • google网站提交入口网站系统优点
  • 郑州网站建设技术托管wordpress性能承载量
  • 网站服务器租赁需要什么手续厦门市建设局网站住房保障专栏
  • 佛山网站建设公司经营范围wordpress 图片展示页面
  • 敦化建设局网站个人网站做多久有效果