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

文件分割技术解决思路

2012-02-15 
文件分割技术longFileSize(FILE*stream)/*求文件长度*/{longcurpos1,curpos2,length/*定位变量和文件长度

文件分割技术
long   FileSize(FILE   *stream)       /*求文件长度*/
{
        long   curpos1,curpos2,length;         /*定位变量和文件长度变量*/
        fseek(stream,0L,SEEK_END);
        curpos1=ftell(stream);                       /*定位在文件开头*/
        fseek(stream,0L,SEEK_END);
        curpos2=ftell(stream);                       /*定位在文件结尾*/
        length=curpos1-curpos2;                     /*计算出文件的长度*/
        fseek(stream,curpos1,SEEK_SET);     /*重新回到文件开头*/
        return   length;                                       /*返回该文件的长度*/
}

int   main(void)
{
        char   inf[10],outfile1[10],outfile2[10];         /*需要分割的文件名字和分别存储在两个文件的文件名字*/    
        FILE   *in,*out1,*out2;                       /*需要分割的文件指针和存储文件的指针*/
        unsigned   long   infile_length,outfile1_length,outfile2_length;     /*分割文件和存储文件的长度变量*/    
        char   *buf;       /*缓冲区*/
/*假设需要的文件都已成功打开...*/
        infile_length=FileSize(in);             /*   元文件长度   */
                                                                                                               
        printf( "\nThe   size   of   the   outfile1   is:\n ");
        scanf( "%d ",&outfile1_length);         /*   用户要求分解到的文件长度   */
   
 
        if(outfile1_length   >   infile_length)
        {
              printf( "[ERROR]:out1fileLength   is   too   large,   Please   try   to   allocate   again!\n ");
              system( "pause ");
              exit(1);
        }
        buf=(char   *)malloc(infile_length);      
        outfile2_length=infile_length-outfile1_length;
        fread(buf,infile_length,1,in);
        fwrite(buf,outfile1_length,1,out1);
        fwrite((char   *)(buf+outfile1_length),outfile2_length,1,out2);
        ...
}

问题:
1.为什么我运行结果总是cpu报错?
2.为什么这里是%d而不是%ld   (     scanf( "%d ",&outfile1_length);  
谢谢!

[解决办法]
应该你是有段错误
也就是指针没用对

------解决方案--------------------


fseek(stream,0L,SEEK_END);
curpos1=ftell(stream); /*定位在文件开头*/
fseek(stream,0L,SEEK_END);
curpos2=ftell(stream); /*定位在文件结尾*/

恕我眼拙。。。没看出区别。。
[解决办法]
2.为什么这里是%d而不是%ld ( scanf( "%d ",&outfile1_length);

应该是 %ld
[解决办法]
1 确认你的文件操作方式都是 2进制方式的,
否则读写的字节数不对 ....

热点排行