Skip to content

Instantly share code, notes, and snippets.

@mcpherrinm
Created February 9, 2013 07:37
Show Gist options
  • Save mcpherrinm/4744509 to your computer and use it in GitHub Desktop.
Save mcpherrinm/4744509 to your computer and use it in GitHub Desktop.
simple cat program that seems broken
use io::ReaderUtil;
fn main() {
let file = path::Path("input");
let f = result::unwrap(io::file_reader(&file));
for f.each_char() |i| {
io::print(fmt!("%c", i));
}
}
@jakerr
Copy link

jakerr commented Feb 9, 2013

A similar program in c will cause a similar error.
Checking feof before fgetc won't stop fgetc from reading the EOF character 0xff once.

#include <stdio.h>

int main(int argc, char const *argv[]) {
    FILE *fin = fopen("input", "r");
    while(!feof(fin)) {
        char c = fgetc(fin);
        putc(c, stdout);
    }
    fclose(fin);
}
$ ./catc |xxd
0000000: 5448 4953 2049 5320 4954 ff              THIS IS IT.

Notice the extra ff on the end.

ReaderUtil's read_char makes the same mistake of checking feof before blindly grabing the next character assuming it won't be the EOF.

I started looking at fixing it but there's a lot of interdependency with read_bytes etc that I don't have time to grok right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment