Something like

    while (n != EOF) {
        n = read(fd, buf, sizeof(buf));
        if (n==-1) throw(...);
        if (strcmp(buf, PREFIX) == 0) {
             return buf;
        }
    }
    return NULL;

Requires no prior knowledge, and have similar effect.


I'm surprised nobody commented on "no prior knowledge". How are you supposed to guess what strcmp(buf, PREFIX) does if you don't know the function? Why is it being compared to 0? What's this -1 magic number? What does read do? What is EOF? Unless you're born with C/POSIX programming knowledge, this is prior knowledge. I know C well enough, but it took me some time to understand what it does.

And honestly, compared to
  File("/tmp/a").byChunk(4096).joiner.startsWith(s)
you can easily guess that you have a file - do some nonobvious magic on it - and check if it starts with `s` just by reading it as plain English.

Reply via email to