79397703

Date: 2025-01-29 17:56:22
Score: 0.5
Natty:
Report link

The scanner's buffer size must be set to a value equal to or greater than the longest line in the file. The application cannot know this size when scanning files with unknown content and size.

If your your actual problem is counting the number of lines in a file, then count newlines in the file:

func countLines(r io.Reader) (int, error) {
    buf := make([]byte, 4086)
    lines := 0
    sep := []byte{'\n'}
    for {
        n, err := r.Read(buf)
        lines += bytes.Count(buf[:n], sep)
        if err == io.EOF {
            return lines, nil
        } else {
            return lines, err
        }
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Talyor Rex