Vissza

C++, állomykezelés
Binárisállomány tartalmának feldolgozása bufferS méretű bájtblokkokban, a "kep.jpg" állomány átmásolása az "ujkep.jpg" állományba:
#include <iostream>
#include <fstream>

using namespace std;
int file_copy(char *inf, char *out);

int main() {
    file_copy("kep.jpg", "ujkep.jpg");
    cout << "succesful copy..." << endl;
    return 0;
}

int file_copy(char *inf, char *outf)
{
    ifstream in(inf, ifstream::binary);
    if (!in.is_open()) {
        cout << "Error opening in_file\n";
        return 0;
    }
    ofstream out(outf, ifstream::binary);
    if (!out.is_open()) {
        cout << "Error opening out_file\n";
        return 0;
    }

    int bufferS, nrBuf;
    cout << "enter the size of byte block: ";
    cin >> bufferS;

    char *bytes = new char[bufferS];
    while (true) {
        in.read(bytes, bufferS);
        nrBuf = in.gcount();
        if (nrBuf == 0) break;
        out.write(bytes, nrBuf);
    }
    out.close();
    in.close();
    return 0;
}