新手求教,简单压缩程序,语法、思路求指正
时间:10-02
整理:3721RD
点击:
简单压缩文件程序。将两个文件(.txt和.jpg)放入一个文件,再从这个文件还原这两个文件。
用二维数组和指针偏移都出现了错误。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2
// #define LINE 1000
// #define CULUMN 1000
struct file_heard
{
char filename[20];
int filelen;
/* char filecontent[LINE][CULUMN];*/
char *connent;
}f_heard[N] = {0}, f_end[N] = {0};
void file_open (FILE *fp ,int len, char name[20] ,char *p)
{
printf("Input filename:\n");
gets( name);
if ((fp = (fopen(name ,"rb"))) == NULL )
{
printf("Cannot open the file :%s\n",name);
exit(0);
}
rewind(fp);
fseek(fp , 0L, SEEK_END);
len = ftell(fp) +1 ;
printf("%d\n",len);
fread(p , 1, len , fp);
}
void file_openw(FILE *fp, char wname [20])
{
printf("Input filename:\n");
gets( wname);
if ((fp = (fopen(wname ,"wb"))) == NULL )
{
printf("Cannot open the file :%s\n",wname);
exit(0);
}
rewind(fp);
}
// void file_save(char str[LINE][CULUMN], FILE *fp)
//
// {
// int i = 0;
// char *p = NULL;
//
// fgets( p ,CULUMN + 1, fp);
// while( p = NULL)
// {
// str[i++][0] = *p;
// printf("%s\n",*p);
// printf("%s\n",str[i]);
// p = NULL;
// fgets( p ,CULUMN -1, fp);
// }
//
//
// }
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp1 = NULL, *fp2 = NULL, *fp3 = NULL,
*fp4 = NULL, *fp5 = NULL;
char name3[20] = {0} , name4[20] = {0}, name5[20] ={0},* p3 = NULL;
int n , len3 = 0;
file_open( fp1, f_heard[0].filelen,f_heard[0].filename, f_heard[0].connent);
// file_save( f_heard[1].filecontent, fp1);
printf("%s%d%s\n", f_heard[0].filename, f_heard[0].filelen,f_heard[0].connent);
fclose(fp1);
file_open( fp2, f_heard[1].filelen,f_heard[1].filename ,f_heard[1].connent);
/* file_save( f_heard[2].filecontent, fp2);*/
fclose(fp2);
file_openw( fp3 ,name3);
for ( n = 0 ; n < 2 ; n++)
{
fwrite(&f_heard[n], f_heard[n].filelen ,1,fp3);
}
fclose(fp3);
file_open( fp3 ,len3, name3,p3);
file_openw( fp4 ,name4);
file_openw( fp5 ,name5);
fread(&f_end[0], f_heard[0].filelen ,1,fp3);
fwrite(&f_end[0], f_heard[0].filelen , 1, fp4);
fread(&f_end[1], f_heard[1].filelen ,1,fp3);
fwrite(&f_end[1], f_heard[1].filelen , 1, fp5);
fclose(fp3);
fclose(fp4);
fclose(fp5);
return 0;
}
用二维数组和指针偏移都出现了错误。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2
// #define LINE 1000
// #define CULUMN 1000
struct file_heard
{
char filename[20];
int filelen;
/* char filecontent[LINE][CULUMN];*/
char *connent;
}f_heard[N] = {0}, f_end[N] = {0};
void file_open (FILE *fp ,int len, char name[20] ,char *p)
{
printf("Input filename:\n");
gets( name);
if ((fp = (fopen(name ,"rb"))) == NULL )
{
printf("Cannot open the file :%s\n",name);
exit(0);
}
rewind(fp);
fseek(fp , 0L, SEEK_END);
len = ftell(fp) +1 ;
printf("%d\n",len);
fread(p , 1, len , fp);
}
void file_openw(FILE *fp, char wname [20])
{
printf("Input filename:\n");
gets( wname);
if ((fp = (fopen(wname ,"wb"))) == NULL )
{
printf("Cannot open the file :%s\n",wname);
exit(0);
}
rewind(fp);
}
// void file_save(char str[LINE][CULUMN], FILE *fp)
//
// {
// int i = 0;
// char *p = NULL;
//
// fgets( p ,CULUMN + 1, fp);
// while( p = NULL)
// {
// str[i++][0] = *p;
// printf("%s\n",*p);
// printf("%s\n",str[i]);
// p = NULL;
// fgets( p ,CULUMN -1, fp);
// }
//
//
// }
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp1 = NULL, *fp2 = NULL, *fp3 = NULL,
*fp4 = NULL, *fp5 = NULL;
char name3[20] = {0} , name4[20] = {0}, name5[20] ={0},* p3 = NULL;
int n , len3 = 0;
file_open( fp1, f_heard[0].filelen,f_heard[0].filename, f_heard[0].connent);
// file_save( f_heard[1].filecontent, fp1);
printf("%s%d%s\n", f_heard[0].filename, f_heard[0].filelen,f_heard[0].connent);
fclose(fp1);
file_open( fp2, f_heard[1].filelen,f_heard[1].filename ,f_heard[1].connent);
/* file_save( f_heard[2].filecontent, fp2);*/
fclose(fp2);
file_openw( fp3 ,name3);
for ( n = 0 ; n < 2 ; n++)
{
fwrite(&f_heard[n], f_heard[n].filelen ,1,fp3);
}
fclose(fp3);
file_open( fp3 ,len3, name3,p3);
file_openw( fp4 ,name4);
file_openw( fp5 ,name5);
fread(&f_end[0], f_heard[0].filelen ,1,fp3);
fwrite(&f_end[0], f_heard[0].filelen , 1, fp4);
fread(&f_end[1], f_heard[1].filelen ,1,fp3);
fwrite(&f_end[1], f_heard[1].filelen , 1, fp5);
fclose(fp3);
fclose(fp4);
fclose(fp5);
return 0;
}
错误
至今没回复,自己都解决了
小编,我现在做数据通信里的数据压缩部分,我想问一下就是基于labview的数据压缩如何编程实现,希望大神不吝赐教,万分感谢