- 论坛徽章:
- 0
|
本人想在文件test.txt中查找作业代号及开始和结束时间。
但在*********之前几行文件程序正常。全部文件或更多的
的出现Abnormal program termination.当文件达到几十MB时就会
出现CPU出现出错的指令。以下程序是何问题,烦请高手指点。谢谢!
如 JOB START COMPLETE
XM00001 00:30:30 20:00:18
XM00002 18:0019 18:0019
......
test.txt
---------------------------------------------------------------------------
CASH_I_0069 XM000001 XM000001 0001 0301 Started at 00.30.30 at node CASH_I_0054 XM000001 XM000001 0001 0301 Completed at 20.00.18
******************************************************
code: 00000000 at node XXXX01.CASH_I_0069 XM000002 XM000002 0001 0301 Started at 18.00.19 at node XXXX01, Pid = 00 XXXX01 root XM000002 XM000002 0001 0301 Completed at 18.00.19 at node XXXX01, * (1) 10486,cau8trk XXXX01 root .CASH_I_0050 Autoscan started at 21.00.28 for 04/03/2006 at 00.00.00, type = Automatic CASH_I_0054 XM000003 XM000003 0001 0301 Started at 20.00.18, code: 00000000 at node XXXX01, CASH_I_0054 XM000003 XM000003 0001 0301 Completed at 20.01.18, code: 00000000 at node XXXX01, Pid = 00068388 10486
---------------------------------------------------------------------------------
程序如下:
#include "stdio.h"
#include "stdlib.h"
#define N 100
int col=0;
typedef struct name
{
char job[10];
char start[10];
char end[10];
}n;
n nm[N];
void write(char *p1,char *p2,int i,char c)
{
char s[8]="",s1;
int j=0;
if(c=='s'){
for(j=0;j<8;j++)
nm[col].job[j]=*(p1+i-19+j);
nm[col].job[8]='\x0';
strcpy(nm[col].start,p2);
col++;}
else{
for(j=0;j<8;j++){
s1=*(p1+i-19+j);
*(s+j)=s1;
}
s[8]='\x0';
for(j=0;j<=col;j++){
i=locate(nm[j].job,s);
if(i!=-1&&nm[j].end){
strcpy(nm[j].end,p2);
printf("\n%s %s %s\n",nm[j].job,nm[j].start,nm[j].end);
break;}
}
}
}
int locate(char *p1,char *p2){
int i=-1,j=0,m=0,n=0;
m=strlen(p1);n=strlen(p2);
while(i<m-1&&j<n-1)
if(*(p1+i)==*(p2+j))
{i++;j++;}
else
{i=i-j+1;j=0;}
if(j>=n-1)
return i-n+1;
else return -1;
}
void strcat1(char *p,char s,int l)
{
p[l]=s;
p[l+1]='\x0';
}
main()
{
FILE *fp1, *fp2;
int i=0 , j=0, len=0;
long loc1=0, loc2=0;
char str1;
char *p2="Started at", *p3="Completed at";
char *p1="";
char p4[8]="";
if((fp1=fopen("test.txt","rb" )==NULL)
{printf("file can not open!\n" ;
exit(0);
}
if((fp2=fopen("result.txt","wb+" )==NULL)
{printf("file can not open!\n" ;
exit(0);}
printf("\n job start time end time\n" ;
while (!feof(fp1)){
str1=fgetc(fp1);
loc1=ftell(fp1);
fputc(str1,fp2);
loc2=ftell(fp2);
len=strlen(p1);
if(len>80)
{
p1=p1+40;
len=strlen(p1);
}
strcat1(p1,str1,len);
if(strlen(p1)>strlen(p2)){
i=locate(p1,p2);
if(i!=-1)
if((*(p1+i-19)=='X')&&(*(p1+i-1 =='M')&&(*(p1+i-2 =='X')&&(*(p1+i-27)=='M')){
j=0;
*(p4+j)='\x0';
while (!feof(fp1)&&(j<9)){
str1=fgetc(fp1);
len=strlen(p4);
strcat1(p4,str1,len);
j++;
}
write(p1,p4,i,'s');
j=0;
*(p1+j)='\x0';*(p4+j)='\x0';
}
}
if(strlen(p1)>strlen(p3)){
i=locate(p1,p3);
if(i!=-1)
if((*(p1+i-19)=='X')&&(*(p1+i-1 =='M')&&(*(p1+i-2 =='X')&&(*(p1+i-27)=='M')){
j=0;
*(p4+j)='\x0';
while (!feof(fp1)&&(j<9)){
str1=fgetc(fp1);
len=strlen(p4);
strcat1(p4,str1,len);
j++;
}
write(p1,p4,i,'e');
j=0;
*(p1+j)='\x0';*(p4+j)='\x0';
}
}
}
fclose(fp1);
fclose(fp2);
} |
|