C++的文件基本操作

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <filesystem>

namespace fs = std::filesystem;

// Function to create a new file
void CreateFile(const std::string& filename) {
    std::ofstream file(filename);
    if (file.is_open()) {
        std::cout << "File created: " << filename << std::endl;
    } else {
        std::cerr << "Error creating file: " << filename << std::endl;
    }
}

// Function to delete an existing file
void DeleteFile(const std::string& filename) {
    if (fs::remove(filename)) {
        std::cout << "File deleted: " << filename << std::endl;
    } else {
        std::cerr << "Error deleting file (not found?): " << filename << std::endl;
    }
}

// Function to search for text in a file
bool SearchInFile(const std::string& filename, const std::string& searchTerm) {
    std::ifstream file(filename);
    std::string line;
    int lineNum = 1;
    bool found = false;

    if (!file.is_open()) {
        std::cerr << "Error opening file for search: " << filename << std::endl;
        return false;
    }

    while (std::getline(file, line)) {
        if (line.find(searchTerm) != std::string::npos) {
            std::cout << "Found at line " << lineNum << ": " << line << std::endl;
            found = true;
        }
        lineNum++;
    }

    if (!found) {
        std::cout << "Search term not found: " << searchTerm << std::endl;
    }
    return found;
}

// Function to modify content in a file
void ModifyFile(const std::string& filename, const std::string& oldText, const std::string& newText) {
    // Read entire file into memory
    std::ifstream inFile(filename);
    if (!inFile.is_open()) {
        std::cerr << "Error opening file for reading: " << filename << std::endl;
        return;
    }

    std::vector<std::string> lines;
    std::string line;
    bool modified = false;

    while (std::getline(inFile, line)) {
        size_t pos = line.find(oldText);
        if (pos != std::string::npos) {
            line.replace(pos, oldText.length(), newText);
            modified = true;
        }
        lines.push_back(line);
    }
    inFile.close();

    if (!modified) {
        std::cout << "Text not found for modification: " << oldText << std::endl;
        return;
    }

    // Write modified content back to file
    std::ofstream outFile(filename);
    if (!outFile.is_open()) {
        std::cerr << "Error opening file for writing: " << filename << std::endl;
        return;
    }

    for (const auto& l : lines) {
        outFile << l << '\n';
    }
    std::cout << "File modified successfully: " << filename << std::endl;
}

// Function to write data to a file (overwrites existing content)
void WriteToFile(const std::string& filename, const std::string& content) {
    std::ofstream file(filename);
    if (file.is_open()) {
        file << content;
        std::cout << "Content written to file: " << filename << std::endl;
    } else {
        std::cerr << "Error writing to file: " << filename << std::endl;
    }
}

// Function to read data from a file
std::string ReadFromFile(const std::string& filename) {
    std::ifstream file(filename);
    std::string content;

    if (file.is_open()) {
        content.assign(
            (std::istreambuf_iterator<char>(file)),
            (std::istreambuf_iterator<char>())
        );
    } else {
        std::cerr << "Error reading file: " << filename << std::endl;
    }
    return content;
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐