在线等待,高分相送!
我有一道考试题目,编的差不多了,但最后一步实现不了。
请高手,帮忙,检查一下,拜托啦!
题目:文件的合并与还原
在外部存储器中存放大量的小文件(如1k左右)时,会造成明显的存储浪费,降低了存储效率.因此,将多个小文件f合并成一个较大的文件F可以提高存储空间利用率,编写这样一段程序,能够对小文件进行合并与还原.
要求: 1、进行合并操作时,F的文件名与一组f的文件名由输入得到;
2、合并操作后.文件F中应存储每个f的全部信息(包括文件内容、名称、大小、访问属性、日期等);
3、进行还原操作时,从文件F中释放出来的每个小文件f都与原来的文件相同(即文件内容、名称、大小、访问属性、日期等信息被完全恢复).
//我已经完成了合并,并实现记录了个文件属性,以文件1信息+文件1内容+文件2信息+文件2内容+......的方式存为newfile.txt文件,但是在还原时(只能还原用上述程序合并后的文件,所以,先选1合并一些文件,同时生成一个新文件,为第二步作准备),多个文件还原总是有错,一般,只能第一个文件能够顺利还原,第二个就有问题。(注意,还原时,要把该目录下的合并前的文件或转移,因为会生成新的文件,以便作比较。)
//此外,还原后的文件,原来保存的属性要怎么写入呢?如果你知道的话,请告诉我!
#include <stdio.h>
#include <iostream.h>
#include <io.h>
#include <process.h>
#include <wchar.h>
#include <time.h>
#include <malloc.h>
#include <string.h>
typedef struct F_Information
{
long FiIn_ftime; /*文件时间*/
char FiIn_attrib; /*查找到的属性*/
int FiIn_fdate; /*文件日期*/
long FiIn_fsize; /*文件大小*/
char FiIn_name[13]; /*文件名要足够长*/
struct F_Information *next;
/////maybe others to be added////
}F_Information,*FileInformation;
int hebing()
{ struct _finddata_t gettogether_file;
long hFile;
char fname[10];
int n,i;
FILE *fp,*in;
;
F_Information *L,*p,*g;
L=(FileInformation)malloc(sizeof(F_Information));
L->next=NULL; //先建立一个带表头结点的单链表
printf("Enter the the number of the files\n");
scanf("%d",&n);
printf("Enter the files' names\n\n");
for(i=0;i<n;i++)//屏幕输出文件属性,并存入链表
{
scanf("%s",&fname);
if( (hFile = _findfirst( fname, &gettogether_file )) == -1L )
printf( "Cann't find the No. %d files in current directory!\n",i+1);
else
{
printf( "Listing of files to be got together\n\n" );
printf( "\nRDO HID SYS ARC FILE DATE %25c SIZE\n", ' ' );
printf( "--- --- --- --- ---- ---- %25c ----\n", ' ' );
printf( ( gettogether_file.attrib & _A_RDONLY ) ? " Y " : " N " );//与运算,取出某一位!!
printf( ( gettogether_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
printf( ( gettogether_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
printf( ( gettogether_file.attrib & _A_ARCH ) ? " Y " : " N " );
printf( " %-12s %.24s %9ld\n",
gettogether_file.name, ctime( &( gettogether_file.time_write ) ), gettogether_file.size );
printf("\n\n");
p=(FileInformation)malloc(sizeof(F_Information)); //生成新结点
///////////////////下面写入信息!!!!!!!
strcpy(p->FiIn_name,gettogether_file.name);
p->FiIn_fsize=gettogether_file.size;
p->FiIn_ftime=gettogether_file.time_write;
p->FiIn_attrib=gettogether_file.attrib;
//////may be others to be added///////
p->next=L->next;L->next=p; //插入到表头
_findclose( hFile );
}
}
g=L;//输出文件属性到新文件
g=g->next;
for(i=0;i<n;i++)
{
if((fp=fopen("newfile.txt","a"))==NULL)//文件指针指向新文件
{
printf("cannot open outfile");
exit(0);
}
else
{
fwrite(&g->FiIn_name,sizeof(g->FiIn_name),1,fp);//size is 13
fwrite(&g->FiIn_fsize,sizeof(g->FiIn_fsize),1,fp);//size is 4
fwrite(&g->FiIn_ftime,sizeof(g->FiIn_ftime),1,fp);//size is 4
fwrite(&g->FiIn_attrib,sizeof(g->FiIn_attrib),1,fp);//size is 1
if((in=fopen(g->FiIn_name,"r"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
while(!feof(in))fputc(fgetc(in),fp); //核心语句
//fwrite(g,sizeof(struct F_Information),1,fp);
g=g->next;
fclose(fp);
}
}
return 0;
}
int fenjie()
{
char ffname[13];
F_Information *L,*p;
FILE *fp,*in;
char attrib_name[13];
long attrib_size;
long attrib_time;
char attrib;
int i;
int sizebackup=0;
char ch;
printf("Please enter the name of the file wants to be broken up\n");
scanf("%s",&ffname);
if((in=fopen(ffname,"rb"))==NULL)//小文件指针指向小文件
{
printf("cannot open the file\n");
exit(0);
}
while(!feof(in))
{
fseek(in,sizebackup,0);
fread(&attrib_name,sizeof(L->FiIn_name),1,in);//读取文件名
cout<<"name is "<<attrib_name<<endl;
//cout<<"WeiZhi is "<<ftell(in)<<endl;
////////////////////////////////////////先去掉//fseek(in,(sizebackup+13L),0);//读取大小
fread(&attrib_size,sizeof(L->FiIn_fsize),1,in);//读取大小
cout<<"size is "<<attrib_size<<endl;//这个对!!!!!!!不容易啊!!!Fainting
//cout<<"WeiZhi is "<<ftell(in)<<endl;
sizebackup+=attrib_size;////////////////////很重要,为循环作准备
fread(&attrib_time,sizeof(L->FiIn_ftime),1,in);//读取时间
cout<<"time is "<<attrib_time<<endl;
//cout<<"WeiZhi is "<<ftell(in)<<endl;
fread(&attrib,sizeof(L->FiIn_attrib),1,in);//读取属性
cout<<"attrib is "<<attrib<<endl;
//cout<<"WeiZhi is "<<ftell(in)<<endl;
if((fp=fopen(attrib_name,"w"))==NULL)//文件指针指向文件
{
printf("cannot open the file\n");
exit(0);
}
else
{
for(i=0;i<attrib_size;i++)
fputc(fgetc(in),fp);
}
sizebackup+=21;
//if(!feof(in))goto ret;
//判断文件是否结束,结束:ok,未结束:继续读取//
fclose(fp);
}
ret: return 0;
}
void main()
{
int select;
printf("What woule u want to do?\n");
printf("If u want to unite files please enter 1 \n");
printf("If u want to break up a file please enter 2 \n");
scanf("%d",&select);
switch (select)
{
case 1:hebing();break;
case 2:fenjie();break;
}
}
问题点数:100、回复次数:9Top
1 楼iampolaris(北极星)回复于 2003-09-03 19:17:28 得分 10
markTop
2 楼ddyqy(七夜)回复于 2003-09-03 19:24:24 得分 0
拜托,说详细一点。mark,我不懂
我用msdn查,mark 不在 C++documention活动子集,是不是,就不能用啊?
Top
3 楼xiaohei728(开心精灵(火炎焱))回复于 2003-09-03 19:48:17 得分 10
错了
不懂这些
我一听文件就头大了Top
4 楼ddyqy(七夜)回复于 2003-09-03 19:55:18 得分 0
我也是啊,不过,就是一些和文件有关的函数和头文件,掌握了,就好了。
都是这道题逼得我,学了一遍文件的知识。
现在,可能是文件指针,没搞明白,有没有高人指点迷津啊!
我合并的文件,每个文件结尾(下一个文件信息之前),都多了一个 怪字符,就是,字母y上面多2个点,搞不明白。Top
5 楼ddyqy(七夜)回复于 2003-09-03 19:57:28 得分 0
或者说就是,函数int fenjie () 中那个循环里面的错误,(即文件指针,实在搞不懂,晕死我了)
有谁,愿意和我讨论的,欢迎加我qq:20197030Top
6 楼ddyqy(七夜)回复于 2003-09-04 00:31:11 得分 0
没有人,帮帮我吗?
自己顶!Top
7 楼syuui(syuui)回复于 2003-09-04 03:04:48 得分 70
//此外,还原后的文件,原来保存的属性要怎么写入呢?如果你知道的话,请告诉我!
不知道你在什么下写的。如果是TC的话,用system()函数调用DOS的attrib命令可以吧。
如果在UNIX下,用exec system call调用chmod命令,或者直接用chmod函数
#include<sys/types.h>
#include<sys/stat.h>
int chmod( char *path , mode_t mode );
y字上面多两个点那个字符,我查了一下谭浩强的《C程序设计》附录里的ACSII表,它的ACSII码是152。
有一个问题:在int hebing()中,你用FILE *fp来指向被写入的大文件。是吧。那为什么你把fopen()放在循环里呢?那个文件只打开一次就够了吧。是不是因为你反复打开了那个文件,造成了多出来一个怪字符呢?Top
8 楼zhangzumiao(梦里水香)回复于 2003-09-04 10:12:40 得分 10
楼主我同情你,Top
9 楼ddyqy(七夜)回复于 2003-09-06 15:57:59 得分 0
主要问题已经解决!
即,已经完成多文件合并及还原,^o^,应该已经算是一个完整的程序了~
只是,创建时间属性的恢复没有完成,不过我想那是作业的要求,对于实际的应用应该没什么关系呀.
哈哈,今天高兴,散分啦,来者有份!Top




