Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Simba ( 16 years ago )
#include <stdio.h>

// тип, описывающий байтовый массив
typedef struct bytearray_struct{
        char* array;
        long size;
} bytearray;


// функция принимает имя файла и выдает его содержимое в виде байтового массива
bytearray* loadFile&#40;char* file_path&#41;
{
        FILE* f = fopen&#40;file_path, "rb"&#41;;
         
        if ( f == NULL )
                return NULL;

        // узнаем размер
        fseek(f, 0, SEEK_END);
        long size = ftell(f);
        fseek(f, 0, SEEK_SET);

        // создаем байтовый массив
        bytearray* ba = new bytearray;
        ba->array = new char[size];
        ba->size = size;

        // считываем данные из файла и закрываем его
        fread(ba->array, size, 1, f);
        fclose(f);

        // возвращаем байтовый массив
        return ba;
}

// сохраняет байтовый массив в файл
int saveToFile&#40;char* file_path, bytearray* ba&#41;
{
        FILE* f = fopen&#40;file_path, "w+"&#41;;
        fwrite(ba->array, ba->size, 1, f);
        fclose(f);
}

int main()
{
        bytearray* ba = loadFile&#40;"/home/anton/Downloads/System Of A Down - Boom.mpeg"&#41;;
        saveToFile&#40;"/home/anton/test.mpeg", ba&#41;;
        delete ba;
}

 

Revise this Paste

Children: 24775
Your Name: Code Language: