> As for formatted input, don’t ever bother with scanf.
With an entire other article about scanf as the link.
edit:
As for your other point, files may not even be seekable, let alone random access. How should that be implemented for a pipe?
> As for formatted input, don’t ever bother with scanf.
With an entire other article about scanf as the link.
edit:
As for your other point, files may not even be seekable, let alone random access. How should that be implemented for a pipe?
If the input is really not random access capable, provide buffering. You could imagine a version of FILE that automatically does this. There could be a some kind of truncate call to make it forget all past input, once you are sure it's not needed.
In an OS you will have the resources to do this (malloc..). In other contexts (embedded), you may not.. but likely in those cases you don't have pipes.
With a bit more we can have full arbitrary backtracking parsing. This parses "<hex> bob | <decimal> fred" or prints the unknown input:
if(
fscan_push(f) && fscanf(f, "%d %e", &n) && fscanf(f, "fred") && fscan_done(f) || fscan_backup(f)) printf("got fred(%d)\n", n);
else if(
fscan_push(f) && fscanf(f, "%x bob", &n) && fscan_done(f) || fscan_backup(f)) printf("got bob(%x)\n", n);
else
printf("got something else '%f'\n", f);
'%e' expect more inputfscan_push: save current location on a stack associated with FILE. Return true.
fscan_done: drop location from stack. Return true. If stack becomes empty, drop input history up to current file position.
fscan_backup: restore to location from stack. Return false.
'%f' in printf prints the rest of the specified FILE on the stdout.