[aclug-L] Re: Weekly C quiz
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Okay, here's the one line answer:
while (fscanf(in_file, "%*[\n]%200[^\n]%*[^\n]", buffer) && !feof(in_file))
There is no way that I know of to specify a field width in a fscanf without
hard-coding the actual value, and MAX_BUF can't be used within a string, so
note that I put the value 200 in the format string.
In the case that the entire line DOES fit in the buffer, you won't have to
worry about stripping the newline character from the end like, as in the
original code.
This version will also ignore blank lines.
What's the prize? :-)
BTW, you've got to stop asking these questions. They're so much fun, I
never get anything else done. ;-)
John
>Here's the next installment of Larry's Weekly C Quiz. This week we're
>looking at buffer overruns. Here's some code:
>
>#define MAX_BUF 201
>...
>char buffer[MAX_BUF];
>FILE *file_in;
>...
>while (fgets(buffer, sizeof(buffer), file_in) != NULL) {
>....
>}
>
>
>If fgets returns a line from file_in's file bigger than MAX_BUF, buffer
>is full at the maximun size, the rest not going into buffer: good. But
>then the next fgets seems to fill buffer with the leftover from the
>input file's last line. How can I "flush" fgets so a longer line's
>leftover doesn't show up in the next fgets?
>
>Larry
-- This is the discussion@xxxxxxxxx list. To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi
- [aclug-L] Weekly C quiz, Larry Bottorff, 2000/03/10
- [aclug-L] Re: Weekly C quiz, Tom Hull, 2000/03/11
- [aclug-L] Re: Weekly C quiz,
John Reinke <=
- [aclug-L] Re: Weekly C quiz, Larry Bottorff, 2000/03/11
- [aclug-L] Re: Weekly C quiz, Tom Hull, 2000/03/11
- [aclug-L] Re: Weekly C quiz, Tom Hull, 2000/03/12
- [aclug-L] Re: Weekly C quiz, John Reinke, 2000/03/12
- [aclug-L] Re: Weekly C quiz, Larry Bottorff, 2000/03/12
- [aclug-L] Re: Weekly C quiz, John Reinke, 2000/03/12
|
|