上一篇博客我们是写到了print的重载,留下了文件,这里我们简单的贴出来,还未完善
namespace file_ {
void newFolder(std::string name = "Test", std::string path = "") { //默认在同级目录下创建文件夹,第一个参数表示文件夹名字,第二个是路径
std::string temp = "md ";
temp += path;
temp += name;
std::cout << "创建文件夹 " << temp << std::endl;
system(temp.data());
}
//以追加模式打开写文件
void newWriteFile(std::string name = "Test.txt", std::string data = "yyds", std::string path = "") {
path += name;
std::ofstream ofs;
ofs.open(path, std::ios::app);
ofs << data;
ofs.close();
}
//创建新的文件,有就删除
void newlyFile(std::string name = "Test.txt", std::string data = "newly", std::string path = "") {
path += name;
std::ofstream ofs;
ofs.open(path, std::ios::trunc);
ofs << data;
ofs.close();
}
//删除文件的数据
void deleteData(std::string name = "Test.txt",std::string path = "") {
path += name;
std::ofstream ofs(path, std::ios::trunc);
ofs.close();
}
//删除文件
void deleteFile(std::string path) {
if (remove(path.data()) == 0) {
std::cout << "删除成功" << std::endl;
}
else {
std::cout << "删除失败" << std::endl;
}
}
//读取文件
std::string readFile(std::string path) {
std::ifstream ifs;
ifs.open(path, std::ios::in);
if (!ifs.is_open())
{
std::cout << "文件打开失败" << std::endl;
return "";
}
std::string data{};
while (ifs >> data);
ifs.close();
return data;
}
}
总体来说还是很简单的,老样子写在命名空间内,没有用到模板和重载,后期完善的时候再看,为了方便我们把实现都写在这个头文件内,所以不用在意。下面我们把测试功能的代码贴出来
#include<iostream>
#include<string>
#include<fstream>
#include<cstdio>
void newFolder(std::string s = "Test", std::string path = "") {
std::string temp = "md ";
temp += path;
temp += s;
std::cout <<"创建文件夹 " << temp << std::endl;
system(temp.data());
}
void newWriteFile(std::string name = "Test.txt", std::string data = "yyds", std::string path = "") {
path += name;
std::ofstream ofs;
ofs.open(path, std::ios::app);
ofs << data;
ofs.close();
}
void deleteData(std::string name = "Test.txt", std::string path = "") {
path += name;
std::ofstream ofs(path, std::ios::trunc);
ofs.close();
}
void deleteFile(std::string path = "") {
if (remove(path.data()) == 0) {
std::cout << "删除成功" << std::endl;
}
else {
std::cout << "删除失败" << std::endl;
}
}
std::string readFile(std::string path) {
std::ifstream ifs;
ifs.open(path, std::ios::in);
if (!ifs.is_open())
{
std::cout << "文件打开失败" << std::endl;
return "";
}
std::string data{};
while (ifs >> data);
ifs.close();
return data;
}
int main()
{
//newFolder("测试","D:\\自用\\");
//newWriteFile("测试.txt", "123456");
//deleteData("测试.txt");
//deleteFile("测试.txt");
std::cout << readFile("测试.txt") << std::endl;
return 0;
}
有兴趣的可以自己再测试测试,那么加上之前写的,我们截止目前总共的代码如下:
#ifndef FUNC_H
#define FUNC_H
#include<vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<iterator>
#include<list>
#include<array>
#include<fstream>
#include<fstream>
#include<cstdio>
namespace sort_ {
void merge(int arr[], int start, int end, int mid, int* temp) {
int i_start = start;
int i_end = mid;
int j_start = mid + 1;
int j_end = end;
int Length = 0;
while (i_start <= i_end && j_start <= j_end) {
if (arr[i_start] < arr[j_start])
temp[Length++] = arr[i_start++];
else
temp[Length++] = arr[j_start++];
}
while (i_start <= i_end) {
temp[Length++] = arr[i_start++];
}
while (j_start <= j_end) {
temp[Length++] = arr[j_start++];
}
for (int i = 0; i < Length; i++) {
arr[start + i] = temp[i];
}
}
void mergeSort(int arr[], int start, int end, int* temp) {
if (start >= end) {
return;
}
int mid = (start + end) / 2;
mergeSort(arr, start, mid, temp);
mergeSort(arr, mid + 1, end, temp);
merge(arr, start, end, mid, temp);
}
//快排
template<typename T>
void quickSort(int left, int right, std::vector<T>& arr) {
if (left >= right)
return;
int i = left, j = right, base = arr[left];//取最左边的数为基准数
while (i < j) {
while (arr[j] >= base && i < j)
j--;
while (arr[i] <= base && i < j)
i++;
if (i < j) {
std::swap(arr[i], arr[j]);
}
}
arr[left] = arr[i];
arr[i] = base;
quickSort(left, i - 1, arr);
quickSort(i + 1, right, arr);
}
template<typename T>
void quickSort(int left, int right, T arr[]) {
if (left >= right)
return;
int i = left, j = right, base = arr[left];//取最左边的数为基准数
while (i < j) {
while (arr[j] >= base && i < j)
j--;
while (arr[i] <= base && i < j)
i++;
if (i < j) {
std::swap(arr[i], arr[j]);
}
}
arr[left] = arr[i];
arr[i] = base;
quickSort(left, i - 1, arr);
quickSort(i + 1, right, arr);
}
//选择
template<typename T>//从小到大升序
void selectSort(T arr[], int len) {
for (int i = 0; i < len; i++) {
int min = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
std::swap(arr[min], arr[i]);
}
}
}
template<typename T>
void Inverted(T n[], int str, int end) { //数组逆置
if (str < end) {
std::swap(n[str], n[end]);
Inverted(n, str + 1, end - 1);
}
return;
}
template<typename T>
int sum(T n[], int start, int end) {
if (start == end)return n[start];
int mid = (start + end) >> 1;
return sum(n, start, mid) + sum(n, mid + 1, end);
}//二分递归,数组求和
double average(int n[], int start, int end) {
return sum(n, start, end) / static_cast<double>(end + 1);
}//二分递归,数组求和
int fib(int n) {
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
}//时间复杂度O(2^n),空间消耗很高
int fib2(int n) {
int f = 0, g = 1;
while (0 < n--) {
g = g + f;
f = g - f;
}
return g;
}//时间复杂度O(n),空间复杂度只需要O(1)
}
namespace find_ {
template<typename Comparable>
int binarySearch(const std::vector<Comparable>& a, const Comparable& x)
{
int low = 0, hight = a.size()-1;
while (low <= hight)
{
int mid = (low + hight) / 2;
if (a[mid] < x) {
low = mid + 1;
}
else if (a[mid] > x) {
hight = mid - 1;
}
else
return mid; //找到的情况
}
return -1;
}
template<typename Comparable>
int binarySearch(const Comparable *a, const Comparable x,Comparable len)
{
int low = 0, hight =len-1 ;
while (low <= hight)
{
int mid = (low + hight) / 2;
if (a[mid] < x) {
low = mid + 1;
}
else if (a[mid] > x) {
hight = mid - 1;
}
else
return mid; //找到的情况
}
return -1;
}
}
namespace pow_ {
double pow_(int x, size_t n)
{
if (n == 0)
return 1;
if (n == 1)
return x;
if (n % 2 == 0)
return pow_(x * x, n / 2);
else
return pow_(x * x, n / 2) * x;
}
double pow_(int x, int n)
{
n = -n;
return 1 / pow_(x, static_cast<size_t>(n));
}
}
namespace maxAmin {
template<typename T,size_t size>
auto max(T(&n)[size]) {
T Max{};
for (size_t i = 0; i < size; i++) {
if (n[i] > Max)Max = n[i];
}
return Max;
}
template<typename T>
auto max(std::vector<T>n) {
T Max{};
for (size_t i = 0; i < n.size(); i++) {
if (n[i] > Max)Max = n[i];
}
return Max;
}
template<typename T, size_t size>
auto min(T(&n)[size]) {
T Min = n[0];
for (size_t i = 1; i < size; i++) {
if (n[i] < Min)Min = n[i];
}
return Min;
}
template<typename T>
auto min(std::vector<T>n) {
T Min = n[0];
for (size_t i = 1; i < n.size(); i++) {
if (n[i] < Min)Min = n[i];
}
return Min;
}
}
namespace show_ {
template<typename T,size_t i>
void print(const T(&n)[i], const std::string s=" ") {
std::copy(std::begin(n),std::end(n), std::ostream_iterator<T, char>(std::cout, s.data()));//s做分隔符
std::cout << std::endl;
}
template<typename T,size_t size>
void print(const std::array<T,size> v, const std::string s = " ") {
std::copy(std::begin(v), std::end(v), std::ostream_iterator<T, char>(std::cout, s.data()));//s做分隔符
std::cout << std::endl;
}
void print(const char* s) {
std::cout << s << std::endl; //重载特殊情况,字符串常量输出
}
template<typename T>
void print(const std::vector<T>n,const std::string s=" ") {
std::copy(std::begin(n), std::end(n), std::ostream_iterator<T, char>(std::cout, s.data()));//s做分隔符
std::endl(std::cout);
}
template<typename T>
void print(const T v) {
std::cout << v << std::endl;
}
template<typename T>
void print(const std::list<T>& L,std::string s=" ") {
for (auto it = L.begin(); it != L.end(); it++) { //list容器版本
std::cout << *it << s;
}
std::cout << std::endl;
}
template<typename...Args> //折叠表达式,用于输出一堆东西
void prints(Args...args) {
((std::cout << args << ","), ...);
}
}
namespace file_ {
void newFolder(std::string name = "Test", std::string path = "") { //默认在同级目录下创建文件夹,第一个参数表示文件夹名字,第二个是路径
std::string temp = "md ";
temp += path;
temp += name;
std::cout << "创建文件夹 " << temp << std::endl;
system(temp.data());
}
//以追加模式打开写文件
void newWriteFile(std::string name = "Test.txt", std::string data = "yyds", std::string path = "") {
path += name;
std::ofstream ofs;
ofs.open(path, std::ios::app);
ofs << data;
ofs.close();
}
//创建新的文件,有就删除
void newlyFile(std::string name = "Test.txt", std::string data = "newly", std::string path = "") {
path += name;
std::ofstream ofs;
ofs.open(path, std::ios::trunc);
ofs << data;
ofs.close();
}
//删除文件的数据
void deleteData(std::string name = "Test.txt",std::string path = "") {
path += name;
std::ofstream ofs(path, std::ios::trunc);
ofs.close();
}
//删除文件
void deleteFile(std::string path) {
if (remove(path.data()) == 0) {
std::cout << "删除成功" << std::endl;
}
else {
std::cout << "删除失败" << std::endl;
}
}
//读取文件
std::string readFile(std::string path) {
std::ifstream ifs;
ifs.open(path, std::ios::in);
if (!ifs.is_open())
{
std::cout << "文件打开失败" << std::endl;
return "";
}
std::string data{};
while (ifs >> data);
ifs.close();
return data;
}
}
#endif
纯属随便写写而已,个人觉得没有什么技术含量,环境是MSVC,C++20,debug,未开启优化,下次再见