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

网站的建设要多少钱广州谷歌优化

网站的建设要多少钱,广州谷歌优化,seo做的比较好的网站,网站的客服一般怎么做目录 路径操作 目录遍历 文件检查和操作 总结 每次写C进行目录操作时,我一般都是调平台的SDK,尤其是win32 api 非常难记,于是查一下文档看看有没有和Python中os模块一样好用的库。 于是发现 filesystem,从来没用过&#xff0…

目录

路径操作

目录遍历

文件检查和操作

总结


每次写C++进行目录操作时,我一般都是调平台的SDK,尤其是win32 api 非常难记,于是查一下文档看看有没有和Python中os模块一样好用的库。

于是发现 filesystem,从来没用过(我的第六版C++ primer 最新标准只介绍了C++11)

用法整理如下,仅供参考

当你需要对计算机上的文件和目录进行操作时,C++17标准库中的<filesystem>头文件可以为你提供方便的工具。它提供了一系列的类和函数来处理文件和目录的操作,包括路径操作、目录遍历、文件检查、文件操作等等。本文将为你介绍<filesystem>头文件的使用方法和功能。

当你需要对计算机上的文件和目录进行操作时,C++17标准库中的<filesystem>头文件可以为你提供方便的工具。它提供了一系列的类和函数来处理文件和目录的操作,包括路径操作、目录遍历、文件检查、文件操作等等。本文将为你介绍<filesystem>头文件的使用方法和功能。

路径操作

<filesystem>头文件中的path类用于表示文件或目录的路径。你可以使用各种运算符和函数来操作路径。

首先,你可以使用/运算符来将路径组合起来。例如:

#include <filesystem>
#include <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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;
}

上面的代码将输出目录中的所有子目录的路径。

C++17 的 <filesystem> 标准库提供了获取当前工作目录和创建目录的功能。

获取当前工作目录的方式是通过 std::filesystem::current_path 函数来实现,它返回一个 std::filesystem::path 类型的对象,表示当前工作目录的路径。例如:

#include <filesystem>
#include <iostream>int 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 <iostream>int 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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 <iostream>namespace 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.tj-hxxt.cn/news/42810.html

相关文章:

  • discuz 做网站可以吗磁力下载
  • 网站建设在360属于什么类目推广文案怎么写吸引人
  • 域名领域百度搜索引擎优化公司哪家强
  • 网页设计模板素材营销型首页四川百度推广和seo优化
  • 用苹果手机做网站东莞seo网络营销
  • 原来做网站后来跑国外了北京网络推广外包公司排行
  • 公司该建哪种网站企业网站有哪些类型
  • 杭州外贸网站建设郑州百度推广开户
  • 网站建设的技术风险分析与规避项目平台
  • 网站开发需求文档模板中关村在线app
  • 浙江杰立建设集团 网站首页中国站长网入口
  • 自己想做个网站怎么做的厦门seo排名
  • 杭州市富阳区建设局网站昭通网站seo
  • 怎么往网站添加视频seo机构
  • 政府门户网站建设的意义sem优化技巧
  • 仿70网站分类目录源码cilimao磁力猫最新版地址
  • 衡水提供网站制作公司哪家专业图们网络推广
  • 店铺设计效果图软件seo专业论坛
  • 阿里云网站建设好用吗南京响应式网站建设
  • 国外网站有备案吗seo百科
  • 网站建设应对客户问题的话术免费技能培训在哪里报名
  • 个人博客网站备案吗百度推广后台登陆官网
  • 做电销要在哪个网站上找资源百度快速收录网站
  • 泉州网站建设维护关键词优化哪家强
  • 有链接的网站怎么做产品推广步骤
  • 类似于美团的网站开发百度快速收录3元一条
  • 日本做暧视频小网站东莞互联网推广
  • 手机手机端网站建设个人网站免费推广
  • 无锡网站制作电话多少人民政府网站
  • 培训网站建设方案说明书网推平台有哪些