一段切割文件的源码,一段重写的日志记录源码 继续散分
先贴切割文件的
cutfile.h:
- C/C++ code
//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 32767typedef 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);#endifcutfile.c
- C/C++ code
#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:
- C/C++ code
//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 32typedef 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
- C/C++ code
//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;}
[解决办法]
没事闲来接分
[解决办法]
厉害。。mark
[解决办法]
怎么啦?
[解决办法]
还是apache的logrotate写的好啊
[解决办法]
楼主加油 。不错。
[解决办法]
好高啊!
楼主多给我点分行吗?我要到3个小三角了。
[解决办法]
接分
[解决办法]
路过接分
[解决办法]
学习,接点分
[解决办法]
楼主多给我点分
[解决办法]
接楼主的银子
[解决办法]
没太仔细看代码,
切割文件有没有这些功能:
有的文件有头信息,
切割的时候,
给每个部分都加上自己的头,比如切bmp文件,
[解决办法]
jf
[解决办法]
接分。
实现的功能算比较简单。
[解决办法]
jf
[解决办法]
jf 顺便帮忙顶一下
[解决办法]
呵呵~~来接分……
[解决办法]
jf~~~