--- /dev/null
+
+#include "btree.h"
+
+static void write_tree_header (BTree *tree);
+static void write_block (BTree *tree, uchar *block, uint num);
+
+BTree *btree_create (const char *filename, unsigned int block_size)
+{
+ BTree *tree;
+ uchar *node;
+
+ tree = malloc (sizeof(BTree));
+ if (!tree)
+ return NULL;
+
+ tree->fp = fopen (filename, "wb");
+ if (!tree->fp) {
+ free (tree);
+ return NULL;
+ }
+
+ /* Nombre de archivo */
+ tree->filename = malloc(sizeof(char)+1);
+ strcpy (tree->filename, filename);
+
+ /* Inicializo el header */
+ tree->header.block_size = block_size;
+ write_tree_header (tree);
+
+ /* Creo el primer bloque vacio */
+ node = calloc (1, block_size);
+ write_block (tree, node, 0);
+
+ return tree;
+}
+
+int btree_close (BTree *tree)
+{
+ fclose (tree->fp);
+ free (tree->filename);
+ free (tree);
+
+ return 0;
+}
+
+static void write_tree_header (BTree *tree)
+{
+ fseek (tree->fp, 0L, SEEK_SET);
+ fwrite (&tree->header, 1, sizeof (BTreeHeader), tree->fp);
+}
+
+static void write_block (BTree *tree, uchar *block, uint num)
+{
+ fseek (tree->fp, num*tree->header.block_size + sizeof (BTreeHeader), SEEK_SET);
+ fwrite (block, 1, tree->header.block_size, tree->fp);
+}
+