在windows和linux上用c++11实现一个目录下多个文件生成一个文件,一个文件生成多个文件

Windows

allToOne.cpp

#include <iostream>
#include <vector>
#include <windows.h>
#include <fstream>

//返回的是文件的名字的容器
std::vector<std::string> listFilesInDirectory(const std::string& path) {
    std::vector<std::string> files;
    HANDLE hFind;
    WIN32_FIND_DATAA ffd;
    std::string searchPath = path + "\\*"; // 在路径末尾添加通配符以搜索所有文件

    hFind = FindFirstFileA(searchPath.c_str(), &ffd);
    if (hFind == INVALID_HANDLE_VALUE) {
        std::cerr << "FindFirstFile failed: " << GetLastError() << std::endl;
        return files;
    }

    do {
        // 跳过目录本身和子目录
        if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
            if (strcmp(ffd.cFileName, "allFiles.txt") != 0) {
                files.push_back(ffd.cFileName);
            }
        }
    } while (FindNextFileA(hFind, &ffd) != 0);

    FindClose(hFind);
    return files;
}
//获取文件行数
int getFileLineCount(const std::string& filePath) {
    std::ifstream file(filePath);
    if (!file) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return 0;
    }

    int lineCount = 0;
    std::string line;
    while (std::getline(file, line)) {
        lineCount++;
    }

    file.close();
    return lineCount;
}
//获取文件内容
std::vector<std::string> getFileContent(const std::string& filePath) {
    std::vector<std::string> content;
    std::ifstream file(filePath,std::ios::app|std::ios::binary|std::ios::out);
    if (!file) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return content;
    }

    std::string line;
    while (std::getline(file, line)) {
        content.push_back(line);
    }

    file.close();
    return content;
}
//删除文件
bool deleteFile(const std::string& filePath) {
    if (std::remove(filePath.c_str()) != 0) {
        std::cout << "Failed to delete file: " << filePath << std::endl;
        return false;
    }

    std::cout << "File deleted successfully: " << filePath << std::endl;
    return true;
}
//合并文件
void createFiles(const std::string& directory){
    std::ofstream outputFile(directory+"/"+"allFiles.txt");
    std::vector<std::string> a=listFilesInDirectory(directory);
    for (int i = 0; i < a.size(); ++i) {
        outputFile<<a[i]<<std::endl;
        std::string c=directory+"/"+a[i];
        outputFile<<getFileLineCount(c)<<std::endl;
        std::vector<std::string> b=getFileContent(c);
        for (int j = 0; j < b.size(); ++j) {
            outputFile<<b[j]<<std::endl;
        }
        deleteFile(c);
    }
    outputFile.close();
}
int main(){
    std::string a="D:\\ALearn\\work\\oneToAll\\test";
//    std::vector<std::string> b=listFilesInDirectory(a);
//    for (int i = 0; i < b.size(); ++i) {
//        std::cout<<b[i]<<std::endl;
//    }
//std::cout<<getFileLineCount(a+"/"+"1.txt")<<std::endl;
    createFiles(a);
}

oneToAll.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//删除文件
bool deleteFile(const std::string& filePath) {
    if (std::remove(filePath.c_str()) != 0) {
        std::cout << "Failed to delete file: " << filePath << std::endl;
        return false;
    }
    std::cout << "File deleted successfully: " << filePath << std::endl;
    return true;
}
//生成文件
void splitFiles(const std::string& dir) {
    std::string a=dir+"/"+"allFiles.txt";
    std::ifstream file(a);
    if (!file) {
        std::cout << "Failed to open file: " << a << std::endl;
        return;
    }
    std::string line;
    while (std::getline(file, line)) {
        std::string fileName=line;
        std::getline(file, line);
        int count=std::stoi(line);
        std::ofstream outputFile(dir+"/"+fileName);
        for (int i = 0; i < count; ++i) {
            std::getline(file, line);
            outputFile<<line<<std::endl;
        }
        outputFile.close();
        }

   file.close();
    deleteFile(a);
}
int main() {
    std::string a = "D:\\ALearn\\work\\oneToAll\\test";
    splitFiles(a);

    return 0;
}

 linux

allToOne.cpp

#include <iostream>
#include <vector>
#include <dirent.h>
#include <fstream>
#include <algorithm>

//返回的是文件的名字的容器
std::vector<std::string> listFilesInDirectory(const std::string& directoryPath) {
    std::vector<std::string> files;
    DIR* directory = opendir(directoryPath.c_str());
    if (directory == nullptr) {
        std::cerr << "无法打开目录: " << directoryPath << std::endl;
        return files;
    }

    dirent* entry;
    while ((entry = readdir(directory)) != nullptr) {
        if (entry->d_type == DT_REG) {  // 只获取常规文件
            std::string fileName = entry->d_name;
            if(fileName!="allFiles.txt"){
                files.push_back(fileName);
            }
            
        }
    }

    closedir(directory);
    // 对文件名进行排序
    std::sort(files.begin(), files.end());
    return files;
}
//获取文件行数
int getFileLineCount(const std::string& filePath) {
    std::ifstream file(filePath);
    if (!file) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return 0;
    }

    int lineCount = 0;
    std::string line;
    while (std::getline(file, line)) {
        lineCount++;
    }

    file.close();
    return lineCount;
}
//获取文件内容
std::vector<std::string> getFileContent(const std::string& filePath) {
    std::vector<std::string> content;
    std::ifstream file(filePath,std::ios::app|std::ios::binary|std::ios::out);
    if (!file) {
        std::cout << "Failed to open file: " << filePath << std::endl;
        return content;
    }

    std::string line;
    while (std::getline(file, line)) {
        content.push_back(line);
    }

    file.close();
    return content;
}
//删除文件
bool deleteFile(const std::string& filePath) {
    if (std::remove(filePath.c_str()) != 0) {
        std::cout << "Failed to delete file: " << filePath << std::endl;
        return false;
    }

    std::cout << "File deleted successfully: " << filePath << std::endl;
    return true;
}
//合并文件
void createFiles(const std::string& directory){
    std::ofstream outputFile(directory+"/"+"allFiles.txt");
    std::vector<std::string> a=listFilesInDirectory(directory);
    for (int i = 0; i < a.size(); ++i) {
        outputFile<<a[i]<<std::endl;
        std::string c=directory+"/"+a[i];
        outputFile<<getFileLineCount(c)<<std::endl;
        std::vector<std::string> b=getFileContent(c);
        for (int j = 0; j < b.size(); ++j) {
            outputFile<<b[j]<<std::endl;
        }
        deleteFile(c);
    }
    outputFile.close();
}
int main(){
    std::string a="/home/xyt/work/vsCode/pro1/3/test";
//    std::vector<std::string> b=listFilesInDirectory(a);
//    for (int i = 0; i < b.size(); ++i) {
//        std::cout<<b[i]<<std::endl;
//    }
// std::cout<<getFileLineCount(a+"/"+"1.txt")<<std::endl;
 createFiles(a);
}

oneToAll.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//删除文件
bool deleteFile(const std::string& filePath) {
    if (std::remove(filePath.c_str()) != 0) {
        std::cout << "Failed to delete file: " << filePath << std::endl;
        return false;
    }
    std::cout << "File deleted successfully: " << filePath << std::endl;
    return true;
}
//生成文件
void splitFiles(const std::string& dir) {
    std::string a=dir+"/"+"allFiles.txt";
    std::ifstream file(a);
    if (!file) {
        std::cout << "Failed to open file: " << a << std::endl;
        return;
    }
    std::string line;
    while (std::getline(file, line)) {
        std::string fileName=line;
        std::getline(file, line);
        int count=std::stoi(line);
        std::ofstream outputFile(dir+"/"+fileName);
        for (int i = 0; i < count; ++i) {
            std::getline(file, line);
            outputFile<<line<<std::endl;
        }
        outputFile.close();
        }

   file.close();
    deleteFile(a);
}
int main() {
    std::string a = "/home/xyt/work/vsCode/pro1/3/test";
    splitFiles(a);

    return 0;
}

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 17:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 17:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 17:12:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 17:12:02       20 阅读

热门阅读

  1. 2023天梯赛

    2024-03-24 17:12:02       17 阅读
  2. 长链接与短链接的理解

    2024-03-24 17:12:02       16 阅读
  3. Mockito.when返回的list长度为0问题解决方法

    2024-03-24 17:12:02       17 阅读
  4. 用I/O口模拟IIC总线协议遇到的一些问题

    2024-03-24 17:12:02       19 阅读
  5. 对原型模式的理解

    2024-03-24 17:12:02       19 阅读
  6. XXL-JOB通过Postman调试本地任务

    2024-03-24 17:12:02       20 阅读
  7. 对象与继承

    2024-03-24 17:12:02       20 阅读
  8. 【R包开发:包的组件】 第4章 包的元数据

    2024-03-24 17:12:02       14 阅读
  9. PTA-6-16 删除单链表的重复结点

    2024-03-24 17:12:02       21 阅读
  10. 深入浅出:大型语言模型(LLM)的全面解读

    2024-03-24 17:12:02       23 阅读
  11. DOcker in Docker 原理与实战代码详解

    2024-03-24 17:12:02       16 阅读
  12. c语言函数大全(O开头)

    2024-03-24 17:12:02       16 阅读