首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

问一个关于文件分解的有关问题

2012-05-24 
问一个关于文件分解的问题我有一个文件,a.dat,它能分解为b.dat还有c.dat文件这里面谁能看出来算法啊?我看

问一个关于文件分解的问题
我有一个文件,a.dat,它能分解为b.dat还有c.dat文件
  这里面谁能看出来算法啊?我看了半天,文件在我的资源里面

[解决办法]

探讨
我有一个文件,a.dat,它能分解为b.dat还有c.dat文件
这里面谁能看出来算法啊?我看了半天,文件在我的资源里面

[解决办法]
a.dat应该是批处理文件,文件的分解就是文件内容的分解,读取文件a,按照算法写入文件b,c。

至于具体的算法,我们不是神仙,按照这种提示就会知道,会学发有用的信息才可以交流
[解决办法]
什么个情况嘛
[解决办法]
探讨
如果文件是普通文件,没有特定的格式,你怎么拆都成。
如果是文本文件,你需要按行为单位来拆分。
如果是记录文件,你需要以记录单位来查分。

如果文件有特点的格式(比如包含文件头等),有特定的应用,如bmp,jpg, wav,.doc, .pdf.你必须知道文件的格式,将数据拆分成完整的记录。再为每个文件生成文件头,否则,拆分后的文件不能打开。

对于某些特殊的文件,拆分后是不能工作的,如……

[解决办法]
C/C++ code
#include <sys\stat.h>#include <io.h>#include <fcntl.h>#include <share.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <string.h>#define MAX_CLU_BYTES 65536FILE *fo;int fh;__int64 offs,offs1;__int64 rvi64;int rv,wrv;unsigned char buf[MAX_CLU_BYTES];char ofn[_MAX_PATH];char offstr[80];void strcpybutcomma(char *t,char *s) {    char c;    while (1) {        c = *s++;        if (','!=c) *t++ = c;        if (0==c) break;    }}void main(int argc,char **argv) {    if (argc<3) {        printf("Copy File Tail.\n");        printf("Usage:\n");        printf("    cft filename.ext offset_begin[-offset_end]\n");        printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");        printf("Note: Byte at offset_end is NOT included.\n");        printf("Example:\n");        printf("    cft abc.rar 12345\n");        printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");        printf("    cft abc.rar 123-12345\n");        printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");        printf("    cft abc.rar 0xAB-0xCD\n");        printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");        return;    }    strcpybutcomma(offstr,argv[2]);    rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);    if (rv==0) {        printf("offset %s is not number\n",argv[2]);        return;    }    fh=_sopen(argv[1],_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYWR);    if (fh==-1) {        printf("_sopen %s errno=%d\n",argv[1],errno);        return;    }    if (rv==1) {        offs1=_filelengthi64(fh);        if (offs1==-1i64) {            printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);            _close(fh);            return;        }    } else {//rv==2        if (offs1<offs) {            printf("%s offset_begin>offset_end error\n",argv[2]);            _close(fh);            return;        }    }    rvi64=_lseeki64(fh,offs,SEEK_SET);    if (rvi64!=offs) {        printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);        _close(fh);        return;    }    sprintf(ofn,"%s-",offstr);    strcat(ofn,argv[1]);    fo=fopen(ofn,"wb");    if (fo==NULL) {        _close(fh);        printf("fopen %s error\n",ofn);        return;    }    cprintf("\n%I64u\r",offs);    while (1) {        rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));        if (rv==0) break;//        if (rv<0) {            fclose(fo);            _close(fh);            printf("_read %s offset %I64u error\n",argv[1],offs);            return;        }        wrv=fwrite(buf,1,rv,fo);        if (wrv!=rv) {            fclose(fo);            _close(fh);            printf("fwrite %s error\n",ofn);            return;        } else {            offs+=rv;            cprintf("%I64u\r",offs);            if (offs>=offs1) break;//        }    }    fclose(fo);    _close(fh);    printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn);} 

热点排行