zoukankan      html  css  js  c++  java
  • C++文件IO操作的简单示例

    CppIODemo1.cpp

    #include <iostream>
    #include <fstream>
    #include <chrono>
    #define INPUT_BUFFER_SIZE 1024 * 1024
    int main()
    {
        using namespace std;
        cout << "Type an input file name: ";
        string filename;
        getline(cin, filename);
        ifstream ifs(filename, ifstream::in | ifstream::binary);
        if (ifs)
        {
            cout << "Type an output file name: ";
            getline(cin, filename);
            ofstream ofs(filename, ofstream::out | ofstream::binary);
            if (ofs)
            {
                cout << "Copying file..." << endl;
                char buffer[INPUT_BUFFER_SIZE];
                chrono::system_clock::time_point startTime = chrono::system_clock::now();
                while (!ifs.eof())
                {
                    streamsize numberOfCharacters = ifs.read(buffer, INPUT_BUFFER_SIZE).gcount();
                    ofs.write(buffer, numberOfCharacters);
                }
                chrono::system_clock::time_point endTime = chrono::system_clock::now();
                float elapsedTime = static_cast<chrono::duration<float, ratio<1, 1>>>(endTime - startTime).count();
                cout << "File copied, elapsed time: " << elapsedTime << endl;
            }
            else
                cerr << "Cannot open output file: '" << filename << "'!" << endl;
        }
        else
            cerr << "Cannot open input file: '" << filename << "'!" << endl;
        return 0;
    }

    CppIODemo2.cpp

    #include <iostream>
    #include <fstream>
    #define INPUT_BUFFER_SIZE 1024 * 1024
    int main()
    {
        std::cout << "Enter an input file name: ";
        std::string filename;
        std::getline(std::cin, filename);
        std::ifstream ifs(filename, std::fstream::in);
        if (ifs.is_open())
        {
            std::cout << "Enter an output file name: ";
            std::getline(std::cin, filename);
            std::ofstream ofs(filename, std::fstream::out);
            if (ofs.is_open())
            {
                char buffer[INPUT_BUFFER_SIZE];
                while (!ifs.eof())
                {
                    std::streamsize numberOfCharacters = ifs.read(buffer, INPUT_BUFFER_SIZE).gcount();
                    std::cout.write(buffer, numberOfCharacters);
                    ofs.write(buffer, numberOfCharacters);
                }
            }
            else
                std::cout << "Cannot open file: " << filename << std::endl;
        }
        else
            std::cout << "Cannot open file: " << filename << std::endl;
        return 0;
    }

    附带一句:http://en.cppreference.com,此站点为C/C++权威参考手册^_^若不习惯英文,可浏览此站点的中文版http://zh.cppreference.com

  • 相关阅读:
    Oracle-11g ASM Fast Mirror Resync特性
    Oracle
    Oracle-19C中的DML重定向(DML Redirection)
    Oracle-重建oraInventory仓库
    Oracle-输出存储在ASM中当前数据库客户端未打开的文件列表
    Oracle-19c特性之刷新数据库缓存中的密码文件信息
    Oracle-DG环境进行failover故障演练
    Oracle-switchover转换DG角色
    论衡中校长郗会锁儿子高考移民西藏事件反映出的诸多问题
    退役后记:春夏篇
  • 原文地址:https://www.cnblogs.com/buyishi/p/9027214.html
Copyright © 2011-2022 走看看