一段切割文件的源码,一段重写的日志记录源码 继续散分

shiweifu 2009-12-05 04:59:26
先贴切割文件的
cutfile.h:

//cutfile.h
#ifndef __CUT_FILE_H__
#define __CUT_FILE_H__


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#define EXPORT __declspec(dllexport)
#define SIZE_4M 1024*4096
#define MAXBUF 32767

typedef struct _cfhead
{
unsigned int count;
unsigned int blocklen;
char filename[MAXBUF];
} cfhead;

extern size_t getfilesize(FILE *f);
extern int cutfile(char *filename,unsigned int len);
extern int pastefile(char *filename,char *nname);
#endif


cutfile.c

#include "cutfile.h"

#define __DEBUG__

size_t getfilesize(FILE *f)
{
size_t oldpos;
size_t newpos;

if(f == NULL)
{
perror("f is NULL\r\n");
return -1;
}
oldpos = ftell(f);
fseek ( f , 0 , SEEK_END );
newpos = ftell(f);
fseek(f,oldpos,SEEK_SET);
return newpos;
}

int cutfile(char *filename,unsigned int len)
{
FILE *src;
FILE *dst;
FILE *inf;
size_t size;
int ret;
char tmp[16];
char newfile[MAXBUF];
char *pbuff;
int i;
cfhead head;

src = fopen(filename,"rb");
size = getfilesize(src);
i = 1;
pbuff = (char*)malloc(len);
while(1)
{
strcpy(newfile,filename);
strcat(newfile,".part.");
strcat(newfile,itoa(i,tmp,10));
ret = fread(pbuff,1,len,src);
if(ret == 0)
{
break;
}
dst = fopen(newfile,"wb+");
fwrite(pbuff,1,ret,dst);
fclose(dst);
i++;
}
head.count = i;
head.blocklen = len;
memset(head.filename,0,MAXBUF);
strcpy(head.filename,filename);

strcpy(newfile,filename);
strcat(newfile,".fni");

inf = fopen(newfile,"wb+");
fwrite(&head,1,sizeof(head),inf);
fclose(inf);

free(pbuff);
fclose(src);


return 0;
}

int pastefile(char *filename,char *nname)
{
FILE *f;
FILE *ff;
cfhead head;
size_t i;
unsigned int len;
char newfile[MAXBUF];
char *pbuff;
size_t ret;
char tmp[16];

f = fopen(filename,"rb");
ff = fopen(nname,"wb+");
fread(&head,1,sizeof(cfhead),f);
fclose(f);

pbuff = (char*)malloc(head.blocklen);

memset(pbuff,0,head.blocklen);

for(i = 1; i < head.count; i++)
{
strcpy(newfile,head.filename);
strcat(newfile,".part.");
strcat(newfile,itoa(i,tmp,10));
f = fopen(newfile,"rb");
ret = fread(pbuff,sizeof(char),head.blocklen,f);
fclose(f);
if(ret == 0)
{
break;
}
fwrite(pbuff,1,ret,ff);
}

free(pbuff);
fclose(ff);
return 0;
}

int main(int argc,char **argv)
{
if(argc != 2)
{
printf("args error!\r\n");
return -1;
}
cutfile(argv[1],SIZE_4M);
pastefile(strcat(argv[1],".fni"),"newfile");
return 0;
}

代码经过VC2008、gcc测试均无问题

再贴改良过的simlog:


//simlog.h
#ifndef __SIMLOG_H__
#define __SIMLOG_H__

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAXBUF 256
#define BLOCKLEN 32
#define TIMELEN 32



typedef struct _simblock
{
char leave[BLOCKLEN];
char value[BLOCKLEN];
char time[BLOCKLEN];
} simblock;

typedef struct _simlog
{
int pos;
int max;
simblock **blocks;
char path[256];
} simlog;


extern simlog* createlog(char *path,unsigned int m);
extern int add2log(simlog *sl,simblock *block);
extern int compareline(char *line,simblock *block);
extern simlog* loadlog(char *path);
extern int savelog(simlog *log);
extern void freelog(simlog *log);

#endif




//simlog.c
#include "simlog.h"

simlog* createlog(char *path,unsigned int m)
{
simlog *log;
log = (simlog*)malloc(sizeof(simlog));
log->max = m == 0? 255 : m;
log->pos = 0;
strcpy(log->path,path);
log->blocks = (simblock**)malloc(log->max * sizeof(simblock*));

return log;
}

simblock* createblock()
{
simblock *b;
b = (simblock*)malloc(sizeof(simblock));
memset(b,0,sizeof(simblock));
return b;
}

//char * trim(char * src)
//{
// int i = 0;
// char *begin = src;
// while(src[i] != '\0'){
// if(src[i] != ' '){
// break;
// }else{
// begin++;
// }
// i++;
// }
// for(i = strlen(src)-1; i >= 0; i--){
// if(src[i] != ' '){
// break;
// }else{
// src[i] = '\0';
// }
// }
// return begin;
//}

char *trim(const char *str)
{
static char line[MAXBUF];
const char *pbegin;
char *p,*pend;

pbegin=str;
while (*pbegin==' ')
pbegin++;

p=line;
while (*p=*pbegin){
if ((*p==' ')&&(*(p-1)!=' '))
pend=p;
p++;pbegin++;
}
if (*(p-1)!=' ') pend=p;
*pend=0;
return line;
}

int getnow(char *timestr)
{
static time_t rawtime;
static struct tm * timeinfo;
static size_t len;

if(timestr == NULL)
{
return -1;
}
time ( &rawtime );
timeinfo = (struct tm*)localtime ( &rawtime );
strcpy(timestr,asctime(timeinfo));
timestr[strlen(timestr) - 1] = '\0';
//len = strlen(timestr) + 1;
//timestr[len - 2] = 0;

return 0;
}

int compareline(char *line,simblock *block)
{
char *pch;
if(line == NULL || block == NULL)
{
return -1;
}
pch = strtok (line,"[]");
strcpy(block->leave,pch);
pch = strtok (NULL, "[]");
strcpy(block->value,pch);
strcpy(block->value,trim(block->value));
pch = strtok (NULL, "[]");
strcpy(block->time,pch);
return 0;
}

int add2log(simlog *sl,simblock *block)
{

if(sl == NULL || block == NULL)
{
perror("sl is NULL or block is NULL");
return -1;
}

if(sl->pos >= (sl->max - 2))
{
sl->max *= 2;
sl->blocks = realloc(sl->blocks,sl->max * sizeof(char *));
}

sl->blocks[sl->pos++] = block;

return 0;
}

simlog* loadlog(char *path)
{
FILE *f;
char str[MAXBUF];
int i;
int count;
simlog *sl;
simblock *block;



f = fopen(path,"rb");

fscanf(f,"%*s %d%*s\r\n",&count);
sl = createlog(path,count * 2);

for(i = 0; i < count; i++)
{
memset(str,0,MAXBUF);
fgets(str,MAXBUF,f);
block = createblock();
compareline(str,block);
add2log(sl,block);
}
return sl;
//f

return 0;
}

int savelog(simlog *sl)
{
FILE *f;
int i;

f = fopen(sl->path,"wb+");

fprintf(f,"[LOGS %d]\r\n",sl->pos);

for(i = 0; i < sl->pos; i++)
{
fprintf(f,"[%s] %s [%s]\r\n",
sl->blocks[i]->leave,
sl->blocks[i]->value,
sl->blocks[i]->time);
}
fclose(f);
return 0;
}

void freelog(simlog *sl)
{
int i;
if(sl == NULL)
{
return ;
}

for(i = 0; i < sl->pos; i++)
{
free(sl->blocks[i]);
}

free(sl);
sl = NULL;
return ;
}

int main()
{
// char buff[32];
int i;
simblock *block;
simlog *mylog;// = createlog("mytest.log",255);
simlog *mylog2;
//创建
mylog = createlog("mytest.log",255);
for(i = 0; i < 500; i++)
{
//添加500条记录
block = createblock();
getnow(block->time);
strcpy(block->leave,"SYSLOG");
strcpy(block->value,"testlog");
add2log(mylog,block);
}
// mylog = loadlog("mytest.log");
// strcpy(mylog->path,"c");
//保存和析构
savelog(mylog);
freelog(mylog);

mylog2 = loadlog("mytest.log");
strcpy(mylog2->path,"myc.log");
savelog(mylog2);
freelog(mylog2);
return 0;
}

...全文
147 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengzheyu 2012-11-01
  • 打赏
  • 举报
回复
111111111111111
rat0_0 2009-12-06
  • 打赏
  • 举报
回复
jf~~~
fthislife 2009-12-06
  • 打赏
  • 举报
回复
呵呵~~来接分……
shiweifu 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 cattycat 的回复:]
接分。
实现的功能算比较简单。
[/Quote]

恩,呵呵
shiweifu 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zxw0521 的回复:]
没太仔细看代码,

切割文件有没有这些功能:
有的文件有头信息,
切割的时候,
给每个部分都加上自己的头,比如切bmp文件,
[/Quote]

开始也想把每个文件头都做到文件最前面的
但文件到底有几个不好确定呀,最后只能生成另外一个文件来保存文件信息,然后进行读取和写入
yeah_aly 2009-12-05
  • 打赏
  • 举报
回复
jf 顺便帮忙顶一下
JJ___JJ 2009-12-05
  • 打赏
  • 举报
回复
jf
cattycat 2009-12-05
  • 打赏
  • 举报
回复
接分。
实现的功能算比较简单。
奇才有点痴 2009-12-05
  • 打赏
  • 举报
回复
jf
ZXW0521 2009-12-05
  • 打赏
  • 举报
回复
没太仔细看代码,

切割文件有没有这些功能:
有的文件有头信息,
切割的时候,
给每个部分都加上自己的头,比如切bmp文件,
hbvanguard 2009-12-05
  • 打赏
  • 举报
回复
接楼主的银子
wafukfge 2009-12-05
  • 打赏
  • 举报
回复
楼主多给我点分
deng1243 2009-12-05
  • 打赏
  • 举报
回复
学习,接点分
  • 打赏
  • 举报
回复
路过接分
zhaixingchen 2009-12-05
  • 打赏
  • 举报
回复
接分
shiweifu 2009-12-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 z569362161 的回复:]
好高啊!

楼主多给我点分行吗?我要到3个小三角了。
[/Quote]

哈哈,好像见贴你就来要分,一会给你几分
z569362161 2009-12-05
  • 打赏
  • 举报
回复
好高啊!

楼主多给我点分行吗?我要到3个小三角了。
macrojj 2009-12-05
  • 打赏
  • 举报
回复
楼主加油 。不错。
do_fork 2009-12-05
  • 打赏
  • 举报
回复
还是apache的logrotate写的好啊
菜鸟二号 2009-12-05
  • 打赏
  • 举报
回复
怎么啦?
加载更多回复(2)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧