向大家求一个文件分割器的c语言写法

向大家求一个文件分割器的c语言写法,第1张

用于分割文件的程序

/*** 文件分割 ***/

/***

运行程序时,如果编绎成的可执行文件名为fdiv.exe

若按字节数分割,命令行例如 " fdiv d:\dire\file.nnn 2.5k "

若按块数分割, 命令行例如 " fdiv d:\dire\file.nnn (25) "

分割结果:

例如 把file.nnn 分高陵成 5 块 结果为file#nnn.001 -->file#nnn.005

! 注意:每次分割不得超过 999 块。而且如果超过 31 块,将不生成批拷贝文件

! 被分割的文件的文件名如果超过 4 个字符, 最好把它改为 <=4 (不算扩展名)

因为 DOS 下文件名只识别前 8 个字符,如 file -->file#nnn

***/

#define BYTE 0

#define PIECE 1 /*** 定义分割类型标识 ***/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

main(int argc,char **argv)

{

void fun (char *s)/*** 扩展名自加函数 ***/戚侍戚

FILE *fp_write,*fp_read,*fp_bat

long num_in=0,byte_piece,pc_byte,total_byte

int i,buffer=0,len,pc_fn=0,pc_float=0,byte_rest=0,sort_div

char *fn_in,fn_out[50],p[]=".000",fn_bat[50],fn_obj_bat[12],str_rest_byte[3],str_pc_fn[3],ch

switch (argc) /*** 命令行检测 ***/

{

case 3 : break

case 2 :

printf ("\n ERROR! you forgot to enter the number\n")exit (0)

case 1 :

printf ("\n ERROR! you forgot to enter the file name and the number\n")

exit (0)

}

fn_in=argv[1]

if ((fp_read=fopen(fn_in,"rb"))==NULL) /*** 打开被分割的文件 ***/

{ printf ("\n ERROR! the file not exists\n")exit (0)}

fseek (fp_read,0L,2)total_byte=ftell (fp_read)rewind (fp_read)

if (*argv[2]=='\(') /*** 检测分割类型 ***/

{

sort_div=PIECE

i=1

while (argv[2][i]>='0'&&argv[2][i]<='9')

num_in=num_in*10+argv[2][i++]-'0' /*** n 为输入的块数 ***/

if (argv[2][i]!='\)'&&argv[2][i])

{ printf ("谈租\n ERROR! the entered NUMBER is invalid\n")exit (0)}

if (num_in>999)

{ printf ("\n ERROR! can not creat more than 999 files\n")exit (0)}

byte_rest=total_byte%num_inbyte_piece=total_byte/num_inif (byte_rest>0) byte_piece++

}

else

{

sort_div=BYTE

i=0

while (argv[2][i]>='0'&&argv[2][i]<='9') { num_in=num_in*10+argv[2][i]-'0'i++}

if (argv[2][i]=='.')

{

i++while (argv[2][i]>='0'&&argv[2][i]<='9') { num_in=num_in*10+argv[2][i]-'0'i++pc_float++}

}

ch=argv[2][i]

if (ch=='K'||ch=='k') num_in*=1024if (ch=='M'||ch=='m') num_in=num_in*1024*1024

if (ch&&ch!='.'&&ch!='k'&&ch!='K'&&ch!='m'&&ch!='M')

{ printf ("\n ERROR! the entered NUMBER is invalid\n")exit (0)}

for (i=1i<=pc_floati++) num_in=num_in/10

byte_piece=num_in

if (total_byte%byte_piece==0) pc_fn=total_byte/byte_pieceelse pc_fn=total_byte/byte_piece+1

if (pc_fn>999)

{ printf ("\n ERROR! can not creat more than 999 files\n")exit (0)}

}

i=0/*** d:\dire\file.nnn --->d:\dire\file#nnn ***/

while (*(fn_in+i))

{

if (*(fn_in+i)!='.') *(fn_out+i)=*(fn_in+i)else *(fn_out+i)='#'i++

}

*(fn_out+i)='\0'

strcpy (fn_bat,fn_out)strcpy(fn_obj_bat,fn_out) strcat (fn_bat,".bat")

/*** d:\dire\file#nnn -->d:\dire\file#nnn.bat ***/

strcat (fn_out,p)len=strlen (fn_out)

/*** d:\dire\file#nnn --->dire#nnn.000 ***/

/*** 分割 ***/

fread (&buffer,1,1,fp_read)pc_fn=0

while (!feof(fp_read))

{

fun (fn_out+len-3) /*** 调用扩展名自加函数 ***/

fp_write=fopen(fn_out,"wb")

pc_byte=0

while (!feof(fp_read))

{

fwrite (&buffer,1,1,fp_write)++pc_bytefread (&buffer,1,1,fp_read)

if (pc_byte==byte_piece) { fclose (fp_write)break}

}

pc_fn++

if (sort_div==PIECE) if (pc_fn==byte_rest) byte_piece--

/*** 若按块分割,前 byte_rest 个块文件均比后面的块文件多 1 个字节 ***/

}

fclose (fp_read)fclose (fp_write)

/**** 生成拷贝文件 ***/

if (pc_fn<=31)

{

fp_bat=fopen(fn_bat,"w")fputs ("copy /b ",fp_bat)

len=strlen (fn_obj_bat)

while (fn_obj_bat[len-1]!='\\'&&len-1>=0) len--i=0

while (fn_obj_bat[len+i]) { fn_obj_bat[i]=fn_obj_bat[len+i]i++} fn_obj_bat[i]='\0'

/*** 分离出目标文件名,(取出 fn_in 的关键词) ***/

strcat (fn_obj_bat,".000")

len=strlen(fn_obj_bat)

for (i=1i<=pc_fni++)

{

fun (fn_obj_bat+len-3) /*** 调用扩展名自加函数 ***/

if (i>1) fputs (" + ",fp_bat)

fputs (fn_obj_bat,fp_bat)

}

len=strlen (fn_in)

while (fn_in[len-1]!='\\'&&len-1>=0) len--i=0

while (fn_in[len+i]) { fn_in[i]=fn_in[len+i]i++} fn_in[i]='\0'

fputs (" ",fp_bat)fputs (fn_in,fp_bat)

fclose (fp_bat)

}

/*** 报告分割结果 ***/

printf ("\n OK! %d files have been created",pc_fn)

if (pc_fn<=31) printf (" AND the batch file\n")

else printf (" NO the batch file\n")

if (byte_rest>0)byte_piece++

if (byte_piece==pc_byte) { printf ("\n ALL files are %ld B\n",byte_piece)exit (0)}

str_pc_fn[0]=pc_fn/100+'0'str_pc_fn[1]=pc_fn/10%10+'0'str_pc_fn[2]=pc_fn%10+'0'str_pc_fn[3]='\0'

switch (sort_div)

{

case PIECE :

if (byte_rest)

{

str_rest_byte[0]=byte_rest/100+'0'str_rest_byte[1]=byte_rest/10%10+'0'

str_rest_byte[2]=byte_rest%10+'0'str_rest_byte[3]='\0'

}

if (byte_rest==1) printf ("\n NO.001 is %ld B\n",byte_piece)

if (byte_rest>1) printf ("\n NO.001 --> NO.%s are %ld B\n",str_rest_byte,byte_piece)

if (pc_fn-byte_rest==1) printf ("\n NO.%s is %ld B\n",str_pc_fn,pc_byte)

if (pc_fn-byte_rest>1)

{ fun (str_rest_byte)printf ("\n NO.%s --> NO.%s are %ld B\n",str_rest_byte,str_pc_fn,pc_byte)}

break

case BYTE :

if (pc_fn==2) printf ("\n NO.001 is %ld B\n",byte_piece)

else

{

/*** 借用 str_rest_byte 存储 pc_fn-- ***/

pc_fn--

str_rest_byte[0]=pc_fn/100+'0'str_rest_byte[1]=pc_fn/10%10+'0'str_rest_byte[2]=pc_fn%10+'0'

printf ("\n NO.001 --> NO.%s are %ld B\n",str_rest_byte,byte_piece)

}

printf ("\n NO.%s is %ld B\n",str_pc_fn,pc_byte)

}

}

/*** 定义扩展名自加函数 ***/

void fun (char *s)

{

int i

if (s[2]<'9') s[2]++

else if (s[1]<'9') { s[2]-=9s[1]++}

else { s[2]-=9s[1]-=9s[0]++}

}

strcat()是追加字符串的函数。

strcat(char[] a,char []b)即将第二个字符串追加到第一个字符串末尾,第二个字符串值不变,第一个变长。

你的理解是对的 但是使用有误!追加时需要保证第一个字符串在加上第二个之后依旧不能溢出。在此题中,你的a[]字符串长度在追加上".txt"之后,会溢出,建议在定义a的时候写上char a[100]保证不溢出。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12557277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存