M
Matt DeFoor
I have some log files that I'm working with that look like this:
1000000000 3456 1234
1000000001 3456 1235
1000020002 3456 1223
1000203044 3456 986
etc.
I'm trying to read the file backwards and just look at the first
column. Here's what I've got so far:
in=fopen(fpath,"rb");
if (in!=NULL) {
fseek(in,0,SEEK_END);
back1line(in); /* function that goes back 1 line */
while (1) {
pos=ftell(in);
fgets(buffer,1024,in);
buffer[strlen(buffer)-1]=0;
printf("line=%s\n", buffer);
len=strlen(buffer);
if ((buffer[len-1] == '\n')||(buffer[len-1] == '\r'))
buffer[len-1] = '\0';
memset(cptrs,0,sizeof(cptrs));
i=0;
cptrs=buffer;
while (cptrs && i<3) {
++i;
cptrs=strchr(cptrs[i-1],' ');
if (cptrs==NULL) {
printf("we break in here\n");
break;
}
*cptrs=0;
++cptrs;
}
lprintf(0,"got 0 = <%d>\n",atoi(cptrs[0]));
lprintf(0,"got 1 = <%d>\n",atoi(cptrs[1]));
lprintf(0,"got 2 = <%d>\n",atoi(cptrs[2]));
fseek(in,pos,0);
if (back1line(in)!=0)
return -1;
}
}
else
rc=1;
fclose(in);
This just prints out the elements of the last line of the file and I'm
not sure why. If I replace the while loop that splits on ' ' with a
while loop that uses strtok to tokenize the string, it works great.
But, it seems to me that the above should work. Then again, what do I
know?
TIA!
-matt
1000000000 3456 1234
1000000001 3456 1235
1000020002 3456 1223
1000203044 3456 986
etc.
I'm trying to read the file backwards and just look at the first
column. Here's what I've got so far:
in=fopen(fpath,"rb");
if (in!=NULL) {
fseek(in,0,SEEK_END);
back1line(in); /* function that goes back 1 line */
while (1) {
pos=ftell(in);
fgets(buffer,1024,in);
buffer[strlen(buffer)-1]=0;
printf("line=%s\n", buffer);
len=strlen(buffer);
if ((buffer[len-1] == '\n')||(buffer[len-1] == '\r'))
buffer[len-1] = '\0';
memset(cptrs,0,sizeof(cptrs));
i=0;
cptrs=buffer;
while (cptrs && i<3) {
++i;
cptrs=strchr(cptrs[i-1],' ');
if (cptrs==NULL) {
printf("we break in here\n");
break;
}
*cptrs=0;
++cptrs;
}
lprintf(0,"got 0 = <%d>\n",atoi(cptrs[0]));
lprintf(0,"got 1 = <%d>\n",atoi(cptrs[1]));
lprintf(0,"got 2 = <%d>\n",atoi(cptrs[2]));
fseek(in,pos,0);
if (back1line(in)!=0)
return -1;
}
}
else
rc=1;
fclose(in);
This just prints out the elements of the last line of the file and I'm
not sure why. If I replace the while loop that splits on ' ' with a
while loop that uses strtok to tokenize the string, it works great.
But, it seems to me that the above should work. Then again, what do I
know?
TIA!
-matt