Ceriousmall
6/14/2011 9:15:00 PM
On Jun 14, 3:23 am, Ike Naar <i...@iceland.freeshell.org> wrote:
> On 2011-06-14, Ceriousmall <divadsm...@gmail.com> wrote:
>
>
>
> > int gotline(char line[], int max)
> > {
> > int c, length;
>
> > for (length = 0; length < max-2 && (c = getchar()) != EOF && c !=
> > '\n'; ++length)
> > line[length] = c;
>
> > if (c == '\n')
> > line[length++] = c;
>
> > line[length] = '\0';
>
> > if (c == EOF)
> > return c;
> > }
>
> The gotline() function does not always return a value.
> What is the purpose of the ``if (c == EOF)'' test?
>
>
>
> > [snip]
> > int main(void)
> > {
> > char line[MAXLINE+1];
> > int at_start, at_end, return_val;
>
> > at_start = cmt_state = qts_state = OUT;
>
> > while (at_start != EOF) {
> > return_val = gotline(line, MAXLINE);
> > at_end = return_val == EOF;
> > delcom(line, IN, OUT);
> > printf("%s", line);
>
> > if (at_end)
> > at_start = return_val;
> > }
> > return 0;
> > }
>
> The last element of line[] is never used; it could be declared as
> line[MAXLINE];
> at_end and return_val are redundant; the following does the same thing:
>
> int main(void)
> {
> char line[MAXLINE+1];
> int at_start;
>
> at_start = cmt_state = qts_state = OUT;
>
> while (at_start != EOF) {
> at_start = gotline(line, MAXLINE);
> delcom(line, IN, OUT);
> printf("%s", line);
> }
> return 0;
> }
>
> The program does not perform well if there are lines longer than MAXLINE
> (you can verify this by setting MAXLINE to a small value, e.g. 32, and
> let the program process itself as input).
>
> Why do you process the input line-by-line anyway? The program only needs
> to buffer a few (probably two) characters, not an entire line; comment
> text can be replaced by spaces on-the-fly, no need to postpone this
> until end-of-line or end-of-comment is seen.
> The line handling makes the program more complicated than necessary.
lots of good info as usual, a difference in perspective really helps
with my coding, i find that its easy to get trapped by your own coding
style. The learning curve never ends..........