君子性非异也,善假于物也。

如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空!
posts - 31, comments - 23, trackbacks - 0, articles - 30
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

简单文件分割(06川大考研最后一题)

Posted on 2006-10-18 16:50 neter 阅读(777) 评论(1)  编辑 收藏 引用 所属分类: 程序设计
闲暇无事,写点代码以打发无聊的时间。
 1#include <stdio.h>
 2#include <stdlib.h>
 3
 4int main(void)
 5{
 6    FILE *in,*out;
 7    
 8    int block;
 9    char *buffer;
10    int i;
11    long size;
12    int num = 1;
13    char src[32],dest[32],temp[32];
14    printf("***********************************************\n");
15    printf("请输入要分割的文件的位置:  ");
16    scanf("%s",src);
17    printf("请输入要分割成的小文件的个数:  ");
18    scanf("%d",&num);
19    printf("请输入要分割后文件保存位置:  ");
20    scanf("%s",dest);
21    if ((in = fopen(src,"rb")) == NULL)
22    {
23        printf("Open File Fail\n");
24        exit(0);
25    }

26    fseek(in,0L,2);
27    size = ftell(in);
28    rewind(in);
29    block = size/num; // 每一个文件的大小
30    buffer = (char*)malloc(block);
31
32    for(i=0;i< num;i++)
33    {
34        char ch[32];
35        strcpy(temp,dest);
36        itoa((i+1),ch,10);
37        strcat(temp,ch);
38        strcat(temp,".dat");
39        if(fread(buffer,block,1,in)!=1)
40        {
41            printf("Read File Error\n");
42            exit(0);
43        }

44        if((out = fopen(temp,"wb")) == NULL)
45        {
46            printf("Open File Fail\n");
47            exit(0);
48        }

49        if(fwrite(buffer,block,1,out)!=1)
50        {
51            printf("Write File Error\n");
52            exit(0);
53        }

54        fclose(out);
55    }

56    fclose(in);
57    free(buffer);
58    printf("Cut Over\n");
59    printf("***********************************************\n");
60    return 0;
61}

62
简单就是美的了。

Feedback

# re: 简单文件分割(06川大考研最后一题)  回复  更多评论   

2008-10-07 17:38 by eurstein
谢谢共享

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理