Re: reading file backwards and parsing

From: Eric Sosman (Eric.Sosman_at_sun.com)
Date: 04/13/04


Date: Tue, 13 Apr 2004 10:04:05 -0400

Matt DeFoor wrote:
>
> Eric Sosman <Eric.Sosman@sun.com> wrote in message news:<407AF82F.17814DB9@sun.com>...
> > One obstacle to diagnosing your difficulty is that we're
> > forced to make guesses about the portions of your code that
> > you didn't provide. You omitted all the declarations (the
                                                           ^^^
> > one I'm most interested in is `cptrs'), and you omitted the
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    My crystal ball is obviously in good working order. Send
me a GIF of your palm and I'll read your future -- for a small
fee, calculated in `double' on the DeathStation 9000 ...

> > Please post the shortest *complete* program [...]
>
> Anyway, here it is. In all its horrid glory:
> [...]
>
> int main () {
> FILE *in;
> int rc,len,i;
> char buffer[100];
> char *cptrs[3];
> long pos;

    So `cptrs' is an array of three pointers, called cptrs[0]
through cptrs[2]. Further along:

> i=0;
> cptrs[i]=buffer;
> while (cptrs[i] && i<3) {

    This test can succeed if `i' is equal to 2 ...

> ++i;

... in which case the next line changes `i' to 3 ...

> (int *)cptrs[i]=strchr(cptrs[i-1],' '); /* typecast change to
> work on Linux */

... and you're now trying to store something into cptrs[3],
which doesn't exist. (The cast is bogus; rather than quieting
a warning, it should have prevented the program from compiling
at all. I suspect you're operating the compiler in a non-
conforming mode; if it's gcc, try using the "-ansi -pedantic"
flags, along with "-W -Wall". The proper way to quiet the
warning would have been to #include <string.h>; without that
inclusion your use of strchr(), strlen(), and memset() is not
only suspect, but flat-out incorrect.)

    Once you try to store something in an array element that
doesn't exist, all bets are off. One quite likely (but not
guaranteed) outcome is that some other variable that just
happens to reside next to cptrs[2] will get clobbered. It's
likely (although again, not guaranteed) that the next-door
neighbor will be one of `pos' or `buffer' -- and if you've
managed to dump garbage in either of those, there's a good
chance your program will misbehave.

> With that said, if anyone knows of a better/more efficient way to
> accomplish what I'm trying to do, that'd be great. Essentially, I'm
> trying to read the log file backwards and compare the first column
> which is a timestamp.

    This really isn't enough of an explanation to support an
informed recommendation. If you're trying to process the entire
log backwards, you'll be much better off reading it in the
forward direction and rearranging the processing. If you're
only interested in the last N lines (for smallish N), you may
do well to make a guess about how long those lines are, fseek()
to a position shortly before the estimated start of the group,
read and store the entire tail end of the file, and then figure
out which lines are which.

-- 
Eric.Sosman@sun.com


Relevant Pages

  • Very slow performance while compiling/using X software on AMD64/Nforce3 7.0-CURRENT
    ... I was able to, for example, playing music while compiling, or using firefox. ... I've spotted weird CPU usage by xorg, or by software supposed to be lightweight. ... I suspect that my configuration could be a problem. ... You can find my kernel config file, my xorg.conf, an output of dmesg, and lspci right there: http://www.calaquendi.org/freebsd/ ...
    (freebsd-current)
  • Re: Sbcl feature - symbol occuring at toplevel is not reported with warning
    ... but sbcl compiles such a file without warning. ... ; compiling V ... ; compiling (PRINC:ONE) ...
    (comp.lang.lisp)
  • Re: Calling a matlab dll in java
    ... I got some warning from compiler as follows: ... C++ exception handler used, ... Specify -GX ... Studio\VC98\include\ostream: while compiling class-template ...
    (comp.soft-sys.matlab)
  • FreeBSD 6.1/AMD64: Celestia 1.3.2/1.4.1 not working?
    ... while compiling works on FreeBSD 6.1-PRE/AMD64, ... 3dsread.cpp:514: warning: unused parameter 'contentSize' ... *** Error code 1 ...
    (freebsd-questions)
  • Re: Word macro warning
    ... Thank you Renee and Doug. ... I enabled the macro and looked at the VBA editor but saw no code there. ... exactly the same warning was displayed when I reopened it. ... I suspect that I could retype the whole document (it would ...
    (microsoft.public.word.vba.general)