论坛首页· 友情链接申请·申请版主· 广告投放· 道具中心· 设为首页· 收藏本站
发新话题
打印

暑假无聊编的小程序

暑假无聊编的小程序

1,文件合并
/*文件合并程序 主要用于合并系统中不能打开的文件或非文本文件 如镜像文件*/
/*Produced by skydragon  08/8/20*/

#include <stdio.h>
#define N 255
void main()
{
FILE *fp1=NULL,*fp2=NULL,*fp3=NULL;
int ch;
char name1[N];
char name2[N];
char name3[N];
printf("----------------------------说明-----------------------------------\n");
printf("文件合并程序 主要用于合并系统中不能打开的文件或非文本文件 如镜像文件\n");
printf("skydragon                                                        08/08/20\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("请输入文件一的路径\n");
gets(name1);
printf("请输入文件二的路径\n");
gets(name2);
fp1 = fopen(name1,"rb");
fp2 = fopen(name2,"rb");
if(fp1==NULL)
  printf("can not open file 1\n");
        if(fp2==NULL)
  printf("can not open file 2\n");
printf("请输入合并后的路径\n");
gets(name3);
fp3 = fopen(name3,"wb");
        if(fp3==NULL)
  printf("can not open file 3\n");
while((ch=fgetc(fp1))!=EOF)
{
  
  fputc(ch,fp3);
}
while((ch=fgetc(fp2))!=EOF)
{
  
  fputc(ch,fp3);
}
printf("OK");
    fclose(fp1);
fclose(fp2);
fclose(fp3);
}


2,文件拆分

// name:文件拆分器
// function:将txt文件拆分成任意大小
// produced by:skydragon
// DATE:08/8/20
#include<stdio.h>
#define N 255
void main()
{
FILE *fp=NULL,*fp1=NULL,*fp2=NULL;
char name1[N];
int ch,sum=0,size,k=0;

printf("name:文件拆分器\nfunction:将txt文件拆分成任意大小\nproduced by:skydragon\nDATE:08/8/20\n\n\n\n");
printf(" 请输入你要拆分的文件的路径\n");
gets(name1);
fp = fopen(name1,"r");
if(fp==NULL)
  printf("can not open file\n");
while((ch=fgetc(fp))!=EOF)
{
        sum+=1;
}
fclose(fp);
printf("文件的大小是%d字节\n",sum);
printf("请输入拆分后其中一个文件的大小(单位字节)\n");
scanf("%d",&size);
fp = fopen(name1,"r");
if(fp==NULL)
  printf("can not open file\n");
fp1 = fopen("拆分1.txt","w");
if(fp1==NULL)
  printf("can not open file\n");
fp2 = fopen("拆分2.txt","w");
if(fp2==NULL)
  printf("can not open file\n");
while(k<=size)
{  
  k+=1;
  ch=fgetc(fp);
  fputc(ch,fp1);
        
}
while((ch=fgetc(fp))!=EOF)
{
        fputc(ch,fp2);
}
fclose(fp);
fclose(fp1);
fclose(fp2);

}

无聊编着玩的 第二个只能对txt文件 其他的没有试过 估计不行
主要是训练文件操作的 没什么大用途
本帖最近评分记录

TOP

good
因为梦想而努力,因为有你而精彩!
mail: qianzongming@gmail.com

TOP

引用:
原帖由 skydragon 于 2008-8-20 23:13 发表
while((ch=fgetc(fp1))!=EOF)
{
  fputc(ch,fp3);
}
这种文件操作方法非常不好,在做大文件数据操作时千万不能这样一个字符一个字符地读写文件,操作效率极低,一个不错的方法是 使用内存缓冲区 ,示例:

char * buf=new char[1024*1024];   //开辟1MB的内存空间作为读写缓冲区
while(!feof(fp1))
{
    int count=fread(buf,1,1024*1024,fp1);   //批量读入源文件数据
    fwrite(buf,1,count,fp2);    //批量写入目标文件数据
}

[ 本帖最后由 risingsun 于 2008-8-21 12:51 编辑 ]

TOP

while(!feof(fp1))
这种判断文件是否结束的方法 有时候会出错的 我以前试过很多次 不是每次都成功的

TOP

引用:
原帖由 skydragon 于 2008-8-21 12:55 发表
while(!feof(fp1))
这种判断文件是否结束的方法 有时候会出错的 我以前试过很多次 不是每次都成功的
那是你使用的有问题,feof是判断文件结尾最保险的办法~~~

看看msdn中feof的说明吧 http://msdn.microsoft.com/zh-cn/xssktc6e.aspx ~~~

TOP

回复 5# 的帖子

可是的确我和一些同学在使用的时候出现过一些问题 VC中尤为明显

TOP

引用:
原帖由 skydragon 于 2008-8-21 17:54 发表
可是的确我和一些同学在使用的时候出现过一些问题 VC中尤为明显
这里面可能出现的错误有好几处,你说说具体是什么问题,再把你那段测试的代码发上来看看,顺便对照msdn看看~~~~

TOP

ps: 千万不要这样写啊: feof(fp)==0 !,大错特错了

TOP

发新话题