- 论坛徽章:
- 0
|
谢谢各位,可以了。
但是我这里合并也没那么简单,并不是把所有行全部合并,而是有规则的,比如碰到什么关键字,连续合并5行,碰到另外的关键字就合并4行,还有其他一些规则。
还要加时间进去,等等。
我自己写的基本的雏形:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*assume the max line char is 4096 */
#define maxline 40960
void split_line(char *, FILE *);
int main(int argc, char **argv)
{
char str[maxline];
FILE *fp = fopen(*++argv,"r+");
if(fp == NULL){
printf("can not find or open this file,please check the file\n");
exit(EXIT_FAILURE);
}
while(fgets(str,maxline,fp) > 0){
split_line(str, fp);
}
}
void split_line(char str[maxline], FILE *fp)
{
int i, j;
if(strstr(str, "Time:") != 0){
for(i =0 ; i< strlen(str) - 1; i++)
{
printf("%c", str[i]);
}
printf("\t");
for(i = 0; i <= 3; i++){
if(fgets(str, maxline,fp) > 0){
for(j = 0; j < strlen(str) - 1; j++)
printf("%c", str[j]);
}
printf("\t");
}
printf("\n");
}
else if(strstr(str,"User@Host") != 0){
for(i =0 ; i< strlen(str) - 1; i++)
{
printf("%c", str[i]);
}
printf("\t");
for(i = 0; i <= 2; i++){
if(fgets(str, maxline,fp) > 0){
for(j = 0; j < strlen(str) - 1; j++)
printf("%c", str[j]);
}
printf("\t");
}
printf("\n");
}
else{
NULL;
}
}
|
|