#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <signal.h>
#include <dirent.h>
int read_file(char **data_buf,int *data_len)
{
int ret,size;
char *p;
FILE *fp = NULL;
fp = fopen("./file.log","rb");
if(NULL == fp){
return -1;
}
//求得文件的大小
fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);
//申请一块能装下整个文件的空间
p = (char*)malloc(sizeof(char)*size);
if(p==NULL){
fclose(fp);
return -1;
}
memset(p,0,sizeof(char)*size);
//读文件
fread(p,1,size,fp);//每次读一个,共读size次
*data_buf=p;
*data_len=size;
fclose(fp);
return 0;
}
char * str_tok( char * src,const char c ,char ** turn )
{
char * s=src, * head_str=s, * p, * p_c ;
int len;
if( src==NULL ) return NULL;
if ( *s != '\0' && strlen(s) != 0 ){
p_c=strchr(s,c);
len=abs( p_c - head_str ); //如果需要处理被查找字符 需要 +sizeof(char)
if (p_c!=NULL){
p = (char*)malloc(sizeof(char)*len+sizeof(char));
if(p==NULL){
return NULL;
}
memset(p,0,sizeof(char)*len+sizeof(char));
*turn=p_c+1;
memcpy(p,head_str,len);
return p;
}else{
return NULL;
}
}
return NULL;
}
int main()
{
char *pcont;
int lenth;
char * res;
char *P_turn;
read_file(&pcont,&lenth);
P_turn=pcont;
while( ( res = str_tok(P_turn, '\n',&P_turn) ) != NULL ){
printf("res=%s\n",res);
free(res);
res=NULL;
sleep(1);
}
free(pcont);
pcont=NULL;
return 0;
}
str_tok函数
最新推荐文章于 2021-04-01 13:53:47 发布