扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
输入文件名叫 abc.txt 你可以改名。
青秀网站建设公司创新互联,青秀网站设计制作,有大型网站制作公司丰富经验。已为青秀超过千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的青秀做网站的公司定做!
输出文件名叫 aaX.txt X=0,1,2,3....
单个文件大小 one_size, 你可以 更改大小。
#includestdio.h
main(){
FILE *fp, *fout;
char nameout[80];
int i,j,k,n;
long size, one_size=1024;
char *buff;
// one_size=one_size * 0.1; //你可以 更改大小
fp = fopen ("abc.txt","rb");
if (fp==NULL) { printf("Error opening file"); return 0;}
fseek (fp, 0, SEEK_END); // non-portable
size=ftell (fp);
printf ("Size of abc.txt: %d bytes.\n",size); //得到大小
rewind(fp);
n=size/one_size;
buff = (char*)malloc(sizeof(char)*one_size);
for (i=0;in;i++)
{
sprintf(nameout,"aa%d.txt",i);
fout = fopen(nameout,"wb");
k = fread (buff,1,one_size,fp);
//if (k != one_size) {printf("read err\n"); return (0);}
fwrite(buff,1,one_size,fout);
fclose(fout);
printf("output in %s\n",nameout);
}; // for i
j = size % one_size;
if (j!=0) {
sprintf(nameout,"aa%d.txt",n);
fout = fopen(nameout,"wb");
k = fread (buff,1,j,fp);
// if (k != j) {printf("read err\n"); return (0);}
fwrite(buff,1,k,fout);
fclose(fout);
printf("output in %s\n",nameout);
}
fclose (fp);
return 0;
}
#includestdio.h
#includestring.h
#includestdlib.h
FILE *in,*out;
char filename[50],ch,cfilename[50];
void space()
{
char ext[6][6]=
{
"a.txt","b.txt","c.txt","d.txt","e.txt","f.txt"
};
unsigned long int n=1,k,byte=0;
unsigned int j=0,i=0;
printf("Please input filename:\n");
scanf("%s",filename);
strcpy(cfilename,filename);
if((in=fopen(filename,"r"))==NULL)
{
printf("Cannot open file\n");
exit(0);
}
printf("please input file size after space(kb);\n");
scanf("%d",n);
n=n*1024;
while(filename[j]!='.')
j++;
filename[j]='\0';
if((out=fopen(strcat(filename,ext[i]),"w"))==NULL)
{
printf("Cannot open file\n");
exit(0);
}
fseek(in,0,2);
k=ftell(in); //k存放当前位置,也就是整个文件的大小
fseek(in,0,0);
while(k0)
{
ch=fgetc(in);
fputc(ch,out);
byte++;
k--;
if(byte==n) //当为要求的大小时执行括号内的语句
{
fclose(out);
byte=0;
strcpy(filename,cfilename);
while(filename[j]!='.')
j++;
filename[j]='\0';
i++;
if((out=fopen(strcat(filename,ext[i]),"w"))==NULL)
{
//生成分割后的文件所在路径
printf("Cannot open file\n");
exit(0);
}
}
}
fclose(in);
printf("File succeed space!\n");
}
void main()
{
printf("now file space!\n");
space();
}
这个代码参考一下,不知道是不是你要求的。
C标准库中提供了一个字符串分割函数strtok();
实现代码如下:
#include stdio.h
#include string.h
#define MAXSIZE 1024
int main(int argc, char * argv[])
{
char dates[MAXSIZE] = "$GPGGA,045950.00,A,3958.46258,N,11620.55662,E,0.115,,070511,,,A*76 ";
char *delim = ",";
char *p;
printf("%s ",strtok(dates,delim));
while(p = strtok(NULL,delim))
{
printf("%s ",p);
}
printf("\n");
return 0;
}
运行结果截图如下:
用于分割文件的程序
/*** 文件分割 ***/
/***
运行程序时,如果编绎成的可执行文件名为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 /*** 定义分割类型标识 ***/
#includestdio.h
#includestdlib.h
#includestring.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_in999)
{ printf ("\n ERROR! can not creat more than 999 files\n"); exit (0); }
byte_rest=total_byte%num_in; byte_piece=total_byte/num_in; if (byte_rest0) 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*=1024; if (ch=='M'||ch=='m') num_in=num_in*1024*1024;
if (chch!='.'ch!='k'ch!='K'ch!='m'ch!='M')
{ printf ("\n ERROR! the entered NUMBER is invalid\n"); exit (0); }
for (i=1;i=pc_float;i++) num_in=num_in/10;
byte_piece=num_in;
if (total_byte%byte_piece==0) pc_fn=total_byte/byte_piece; else pc_fn=total_byte/byte_piece+1;
if (pc_fn999)
{ 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_byte; fread (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=1;i=pc_fn;i++)
{
fun (fn_obj_bat+len-3); /*** 调用扩展名自加函数 ***/
if (i1) 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_rest0)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_rest1) 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_rest1)
{ 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]-=9; s[1]++; }
else { s[2]-=9; s[1]-=9; s[0]++; }
}
文件是你分割的,想合并有什么难题呢?
顺次读取原分割好的多个文件,写入到一个文件就可以了。
我们可以看看winrar文件是如何分割文件的。分割后的每个文件文件名有规律,从01开始编号。顺次读取指定文件夹中的文件,如果文件不存在了,说明读入结束了。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流