c语言实现文件循环存储和查找
时间: 2024-01-19 18:02:09 浏览: 93
C语言实现的文件存储引擎
4星 · 用户满意度95%
下面是一个简单的C语言实现文件循环存储和查找的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLOCK_SIZE 1024 // 定义数据块的大小为1024字节
#define MAX_BLOCKS 1000 // 定义最大数据块数量为1000个
typedef struct {
int id; // 数据块的唯一标识符
int next; // 指向下一个数据块的指针
char data[BLOCK_SIZE]; // 存储数据的数组
} DataBlock;
DataBlock blocks[MAX_BLOCKS]; // 数据块数组
int free_head = 0; // 空闲数据块的头指针
int used_head = -1; // 已使用数据块的头指针
int num_blocks = 0; // 当前已使用的数据块数量
// 初始化数据块数组
void init_blocks() {
int i;
for (i = 0; i < MAX_BLOCKS; i++) {
blocks[i].id = i;
blocks[i].next = i + 1;
}
blocks[MAX_BLOCKS - 1].next = 0;
}
// 在空闲数据块链表中分配一个数据块
int allocate_block() {
if (free_head == 0) {
return -1; // 空间已满,无法分配
}
int new_block = free_head;
free_head = blocks[free_head].next;
blocks[new_block].next = -1;
return new_block;
}
// 释放一个已使用的数据块到空闲链表中
void free_block(int block_id) {
blocks[block_id].next = free_head;
free_head = block_id;
}
// 将文件写入数据块链表中
int write_file(char *filename) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
return -1; // 打开文件失败
}
int num_blocks = 0;
int file_size = 0;
int block_id = allocate_block();
int prev_block_id = -1;
while (block_id != -1) {
blocks[block_id].next = allocate_block();
num_blocks++;
if (fread(blocks[block_id].data, BLOCK_SIZE, 1, fp) != 1) {
break; // 文件读取完成
}
file_size += BLOCK_SIZE;
prev_block_id = block_id;
block_id = blocks[block_id].next;
}
fclose(fp);
if (prev_block_id != -1) {
blocks[prev_block_id].next = used_head;
}
used_head = allocate_block();
blocks[used_head].next = used_head;
blocks[used_head].id = num_blocks;
return file_size;
}
// 从数据块链表中读取文件
int read_file(char *filename, int file_size) {
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
return -1; // 创建文件失败
}
int block_id = used_head;
int bytes_read = 0;
while (bytes_read < file_size && block_id != -1) {
if (fwrite(blocks[block_id].data, BLOCK_SIZE, 1, fp) != 1) {
break; // 文件写入失败
}
bytes_read += BLOCK_SIZE;
block_id = blocks[block_id].next;
}
fclose(fp);
return bytes_read;
}
// 释放数据块链表中的文件
int delete_file(int num_blocks) {
int block_id = used_head;
int prev_block_id = -1;
while (block_id != -1 && blocks[block_id].id != num_blocks) {
prev_block_id = block_id;
block_id = blocks[block_id].next;
}
if (block_id == -1) {
return -1; // 文件不存在
}
if (prev_block_id != -1) {
blocks[prev_block_id].next = blocks[block_id].next;
} else {
used_head = blocks[block_id].next;
}
while (block_id != -1) {
int next_block_id = blocks[block_id].next;
free_block(block_id);
block_id = next_block_id;
}
return 0;
}
int main() {
init_blocks(); // 初始化数据块数组
int file_size = write_file("test.txt"); // 将文件写入数据块链表中
if (file_size == -1) {
printf("Failed to open file!\n");
return -1;
}
printf("File saved successfully!\n");
int bytes_read = read_file("test_copy.txt", file_size); // 从数据块链表中读取文件
if (bytes_read == -1) {
printf("Failed to create file!\n");
return -1;
}
printf("File read successfully!\n");
delete_file(1); // 释放数据块链表中的文件
printf("File deleted successfully!\n");
return 0;
}
```
此代码仅是一个简单的示例,实际应用中需要增加更多的错误处理和异常处理代码。
阅读全文