Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • P project00
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • cs3-19sp
  • project00
  • Issues
  • #3

Something went wrong while setting issue due date.
Closed
Open
Created 6 years ago by Matthew E. Wu@mewuMaintainer
  • New issue

  • Report abuse

  • New issue

  • Report abuse

wc double-counts lines due to using feof?

Closed

wc double-counts lines due to using feof?

It seems like the intended solution to wc is

while (!feof(f)) {
    fread(result, 1, 1, f);
    if (result[0] == '\n') {
        num_lines++;
    }
}

But I think in C it's more idiomatic to check the return value of file reading functions like fread than to use feof. The reason is feof returns true only if you read past end of file using another function. Thus the last fread will read nothing and not update result[0], so the last result[0] will be checked twice. This leads to a bug: if the last character in a file is \n, the program will actually count 1 more than the number of newlines. (Thus it will count 2 lines for data/one and 3 lines for data/two.) Also see here: https://stackoverflow.com/a/12337620.

Here's a version which uses the return value of fread and doesn't double count.

while (fread(result, 1, 1, f) == 1) {
    if (result[0] == '\n') {
        num_lines++;
    }
}
  1. Oh no!

    You are trying to upload something other than an image. Please upload a .png, .jpg, .jpeg, .gif, .bmp, .tiff or .ico.

    Incoming!

    Drop your designs to start your upload.

Linked issues
0


  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    The wc they write should match what the real wc does which is what the current choice does, I believe.

  • Matthew E. Wu
    Matthew E. Wu @mewu · 6 years ago
    Author Maintainer

    But wc -l data/one returns 1 and wc -l data/two returns 2.

  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    i'm not sure what to do about this, because i really don't want to introduce that pattern at the beginning; @mewu @csander thoughts?

  • Caleb C. Sander
    Caleb C. Sander @csander · 6 years ago
    Maintainer

    Could they just do this whole assignment with getc() instead? Then they can just compare its return value with EOF.

    Although I guess that defeats the purpose of having to allocate a buffer to store the result.

    Edited by Caleb C. Sander 6 years ago
  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    the other thing is my implementation passed; i think you just need to increment in a different place to ignore the last newline

  • Caleb C. Sander
    Caleb C. Sander @csander · 6 years ago
    Maintainer

    Or they could just put the feof() call after the fread().

  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    yeah that works too. let's just leave it the way it is...hopefully it won't be too confusing, but it'll make for a nice "why is this output wrong" "how do i check" moment.

  • Adam Blank @blank closed 6 years ago

    closed

  • Caleb C. Sander
    Caleb C. Sander @csander · 6 years ago
    Maintainer

    I think this is going to be quite confusing for them to debug. I would infer from "returns non-zero if we've run out of characters to read in the file and 0 otherwise" that it works like hasNext(). If I noticed the line numbers were off, I would think it was a bug on my part, not a misunderstanding of how the functions work. I would do one of the following:

    • Remove the description of feof() and just say that the return value of fread() tells you how many characters were read (which may be less than number_to_read if the end of the file is reached).
    • Change the description of feof() to say that it returns non-zero if the last call to fread() tried to read past the end of the file.
  • Adam Blank @blank reopened 6 years ago

    reopened

  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    i edited the description to match your second suggestion. let me know if you're happy with it now. :)

  • Caleb C. Sander
    Caleb C. Sander @csander · 6 years ago
    Maintainer

    Looks good to me

  • Caleb C. Sander @csander closed 6 years ago

    closed

  • Adam Blank
    Adam Blank @blank · 6 years ago
    Owner

    awesome

  • Adam Blank @blank reopened 6 years ago

    reopened

  • Adam Blank @blank closed 6 years ago

    closed

  • You're only seeing other activity in the feed. To add a comment, switch to one of the following options.
Please register or sign in to reply
0 Assignees
Assign to
Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Due date
None
None
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Confidentiality
Not confidential
Not confidential

You are going to turn on confidentiality. Only team members with at least Reporter access will be able to see and leave comments on the issue.

Lock issue
Unlocked
0
0 participants
Reference: