代码如下:
#define IN_LEN (6400*1024L)
FILE* fp2;
FILE* fp3;
errno_t err; //错误变量的定义
lzo_bytep in;
lzo_bytep out1;
lzo_uint out_len;
lzo_uint file_len;
lzo_uint new_len;
err = fopen_s(&fp2, "文件1.txt", "r+");//打开欲解压的文件
if (err)
{
printf("打开原始文件失败! \n");
return 1;
}
//获取文件长度
long cur = ftell(fp2);
fseek(fp2, 0L, SEEK_END);
file_len = ftell(fp2);
fseek(fp2, cur, SEEK_SET);
//将文件读取到内存中
out1 = (lzo_bytep)malloc(file_len);
int err1 = fread(out1, file_len, 1, fp2);
if (err1 == file_len)
{
printf("读取%lu个字符", file_len);
}
else if (err1 == 0)
printf("读到文件末尾或错误");
//开辟解压后的数据存放空间
in = (lzo_bytep)malloc(IN_LEN);
//解压数据
int r = lzo1x_decompress(out1, out_len, in, &new_len, NULL);
if (r == LZO_E_OK)
{
printf("解压成功:decompressed %lu bytes back into %lu bytes\n",
(unsigned long)out_len, (unsigned long)new_len);
char* uDestName = "解压后文件.txt";
//创建一个文件用来写入解压缩后的数据
err = fopen_s(&fp3, uDestName, "w+b");
if (err)
{
printf("解压缩文件创建失败! \n");
getchar();
return 1;
}
printf("写入数据... \n");
fwrite(in, new_len, 1, fp3);
fclose(fp3);
}
else
{
// this should NEVER happen
printf("解压失败:internal error - decompression failed: %d\n", r);
getchar();
return 1;
}
lzo_free(in);
printf("Simple compression test passed.\n");
getchar();
return 0;
}
整个过程就是读取文件到内存,解压后再写入文件,但是怎么调试都会报:读取位置时发生访问冲突,请大神们帮忙看看到底哪里有问题。