Re: advice needed



Al Balmer <albalmer@xxxxxxx> writes:

On Tue, 07 Mar 2006 23:30:26 GMT, John Smith <JSmith@xxxxxxxx> wrote:

Al Balmer wrote:
On Tue, 07 Mar 2006 19:50:28 GMT, John Smith <JSmith@xxxxxxxx> wrote:


I've written a program in C that does statistical analysis of
text files. The program fscanf()s strings (words) into a
statically allocated 32-byte buffer. 32 bytes is enough to
accomodate the longest English word, but if the input file
contains a non-word string longer than 32 bytes the program will
seg fault. I could increase the buffer to some huge size but
that's wasteful and doesn't solve the problem anyway: there could
always be some huge string 1 byte bigger than my huge buffer.

AFAIK, it's not possible to "trap" a seg fault. If this was your
project, how would you deal with the problem?

JS


Don't use fscanf(). It's the wrong tool for the job.


1. Why is it the wrong tool?

For the reason you stated above, for one.

Although, he could use the field width limiter:

fscanf(file, "%31s", str);

But that's hardly the handiest solution...
.



Relevant Pages

  • Re: advice needed
    ... statically allocated 32-byte buffer. ... accomodate the longest English word, ... always be some huge string 1 byte bigger than my huge buffer. ... Sorry, Phlip, but have you phlipped? ...
    (comp.programming)
  • Re: advice needed
    ... statically allocated 32-byte buffer. ... accomodate the longest English word, ... always be some huge string 1 byte bigger than my huge buffer. ... it's not possible to "trap" a seg fault. ...
    (comp.programming)
  • Re: advice needed
    ... 32 bytes is enough to accomodate the longest English word, but if the input file contains a non-word string longer than 32 bytes the program will seg fault. ... I could increase the buffer to some huge size but that's wasteful and doesn't solve the problem anyway: there could always be some huge string 1 byte bigger than my huge buffer. ... it's not possible to "trap" a seg fault. ...
    (comp.programming)
  • Re: advice needed
    ... 32 bytes is enough to accomodate the longest English word, but if the input file contains a non-word string longer than 32 bytes the program will seg fault. ... I could increase the buffer to some huge size but that's wasteful and doesn't solve the problem anyway: there could always be some huge string 1 byte bigger than my huge buffer. ... it's not possible to "trap" a seg fault. ...
    (comp.programming)
  • Re: advice needed
    ... statically allocated 32-byte buffer. ... accomodate the longest English word, ... always be some huge string 1 byte bigger than my huge buffer. ... it's not possible to "trap" a seg fault. ...
    (comp.programming)